<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh" lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta name="Keywords" content="Zope, Plone, bfg, 开源社区">
<meta content="这里是Zope开源web应用服务器和Plone开源内容管理系统的中文技术社区。" name="description">
<!-- Plone ECMAScripts --><style type="text/css">
            .tabber{display:none}
        </style>
<style type="text/css">
            <!-- @import url(http://everydo.com/themes/czug.org/css/ploneStyles6234.css); -->
        </style>
<link rel="alternate stylesheet" type="text/css" media="screen" href="http://everydo.com/themes/czug.org/css/ploneStyles0053.css" title="Small Text">
<link rel="alternate stylesheet" type="text/css" media="screen" href="http://everydo.com/themes/czug.org/css/ploneStyles2144.css" title="Large Text">
<style type="text/css" media="all">
            <!-- @import url(http://everydo.com/themes/czug.org/css/ploneStyles8099.css); -->
        </style>
<link rel="stylesheet" href="http://everydo.com/themes/czug.org/css/navtree.css" type="text/css">
<script type="text/javascript" src="http://everydo.com/resources/thecmsface/js/sarissa.js">
        </script><script type="text/javascript" src="http://everydo.com/resources/thecmsface/js/base2-dom-fp.js">
        </script><script type="text/javascript" src="http://everydo.com/resources/thecmsface/js/kukit.js">
        </script><script type="text/javascript" src="http://everydo.com/resources/thecmsface/js/zopen.js">
        </script><link type="text/css" rel="kinetic-stylesheet" href="http://czug.org/static/navtree.kss" media="all">
<!-- Internet Explorer CSS Fixes --><!--[if IE]>
            <style type="text/css" media="all">
                @import url(IEFixes.css);
            </style>
        <![endif]--><link rel="shortcut icon" type="image/x-icon" href="http://everydo.com/themes/czug.org/img/favicon.ico">
<link rel="home" href="http://czug.org" title="Front page">
<link rel="search" href="http://everydo.com/themes/czug.org/search_form" title="Search this site">
<link rel="author" href="http://everydo.com/themes/czug.org/author/panjy" title="Author information">
<link rel="contents" href="http://everydo.com/sitemap" title="Site Map">
<!-- Disable IE6 image toolbar --><meta http-equiv="imagetoolbar" content="no">
<title>第十八章 使用wxPython的其他功能 - 中国Zope/Plone用户组</title>
</head>
<body class="section-index_html" dir="ltr">
        <div id="visual-portal-wrapper">
            <div id="portal-top">
                <div id="portal-header">
                    <p class="hiddenStructure">
                        <a accesskey="2" href="http://everydo.com/themes/czug.org/index.html?notheme#documentContent">
                            Skip to content.
                        </a>
                        |
                        <a accesskey="6" href="http://everydo.com/themes/czug.org/index.html?notheme#portlet-navigation-tree">
                            Skip to navigation
                        </a>
                    </p>
                    <h1 id="portal-logo">
                        <a href="http://czug.org" accesskey="1">
                            中国Zope/Plone用户组
                        </a>
                    </h1>
                    <div id="portal-skinswitcher">
                    </div>
                    <h5 class="hiddenStructure">
                        Sections
                    </h5>
                    <ul id="portal-globalnav">
<li id="首页" class="plain">
                        <a href="/">首页</a>
                      </li>
<li id="Zope" class="plain">
                        <a href="/zope">Zope</a>
                      </li>
<li id="Plone" class="plain">
                        <a href="/plone">Plone</a>
                      </li>
<li id="Python" class="selected">
                        <a href="/python">Python</a>
                      </li>
<li id="社区项目" class="plain">
                        <a href="/projects">社区项目</a>
                      </li>
<li id="博客群" class="plain">
                        <a href="/blog/">博客群</a>
                      </li>
<li id="关于" class="plain">
                        <a href="/about">关于</a>
                      </li>
</ul>
</div>
                <div id="portal-personaltools-wrapper">
                    <h5 class="hiddenStructure">
                        Personal tools
                    </h5>
                </div>
                <!-- <div id="portal-breadcrumbs">
                <span id="breadcrumbs-you-are-here">You
                are here:</span>
                <a href="http://czug.org">Home</a>
                </div>
                -->
            </div>
            <div class="visualClear" id="clear-space-before-wrapper-table">
                <!-- -->
            </div>
            <table id="portal-columns"><tbody><tr id="portal-tr">
<td id="portal-column-one">
            <div class="visualPadding"> <div class="document">

<div id="leftcolumn" class="edoBox">
  <b class="xtop edoxtop">
    <b class="xb1"></b>
    <b class="xb2"></b>
    <b class="xb3"></b>
    <b class="xb4"></b>
 </b>
 <b class="itemxtop edoitemxtop">
    <b class="itemxb1"></b>
    <b class="itemxb2"></b>
    <b class="itemxb3"></b>
    <b class="itemxb4"></b>
 </b>
 <dl class="portlet portlet-navigation-tree">
<dt class="portletHeader">
        <a class="title" href="http://www.czug.org/python/wxpythoninaction/">《活学活用wxPython》</a>
    </dt>
    <dd class="portletItem">
        <ul class="portletNavigationTree navTreeLevel0">
<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/pr01.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="pr01.htm" href="http://www.czug.org/python/wxpythoninaction/pr01.htm" onfocus="this.blur()">
        
        
        前言
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/pr02.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="pr02.htm" href="http://www.czug.org/python/wxpythoninaction/pr02.htm" onfocus="this.blur()">
        
        
        关于本书
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch01.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch01.htm" href="http://www.czug.org/python/wxpythoninaction/ch01.htm" onfocus="this.blur()">
        
        
        第一章 欢迎使用wxPython
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch02.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch02.htm" href="http://www.czug.org/python/wxpythoninaction/ch02.htm" onfocus="this.blur()">
        
        
        第二章 给wxPython程序一个坚实的基础
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch03.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch03.htm" href="http://www.czug.org/python/wxpythoninaction/ch03.htm" onfocus="this.blur()">
        
        
        第三章 在事件驱动环境中开发
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch04.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch04.htm" href="http://www.czug.org/python/wxpythoninaction/ch04.htm" onfocus="this.blur()">
        
        
        第四章 用PyCrust使得wxPython更易处理
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch05.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch05.htm" href="http://www.czug.org/python/wxpythoninaction/ch05.htm" onfocus="this.blur()">
        
        
        第五章 绘制蓝图
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch06.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch06.htm" href="http://www.czug.org/python/wxpythoninaction/ch06.htm" onfocus="this.blur()">
        
        
        第六章 使用wxPython基本构件
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch07.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch07.htm" href="http://www.czug.org/python/wxpythoninaction/ch07.htm" onfocus="this.blur()">
        
        
        第七章 使用基础控件
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch08.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch08.htm" href="http://www.czug.org/python/wxpythoninaction/ch08.htm" onfocus="this.blur()">
        
        
        第八章 将构件放入窗体中
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch09.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch09.htm" href="http://www.czug.org/python/wxpythoninaction/ch09.htm" onfocus="this.blur()">
        
        
        第九章 通过对话框让用户选择
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch10.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch10.htm" href="http://www.czug.org/python/wxpythoninaction/ch10.htm" onfocus="this.blur()">
        
        
        第十章 创建和使用wxPython菜单
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch11.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch11.htm" href="http://www.czug.org/python/wxpythoninaction/ch11.htm" onfocus="this.blur()">
        
        
        第十一章 使用sizer放置构件
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch12.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch12.htm" href="http://www.czug.org/python/wxpythoninaction/ch12.htm" onfocus="this.blur()">
        
        
        第十二章 操作基本图像
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch13.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch13.htm" href="http://www.czug.org/python/wxpythoninaction/ch13.htm" onfocus="this.blur()">
        
        
        第十三章 建造列表控件并管理列表项
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch14.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch14.htm" href="http://www.czug.org/python/wxpythoninaction/ch14.htm" onfocus="this.blur()">
        
        
        第十四章 网格控件
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch15.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch15.htm" href="http://www.czug.org/python/wxpythoninaction/ch15.htm" onfocus="this.blur()">
        
        
        第十五章 树形控件
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch16.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch16.htm" href="http://www.czug.org/python/wxpythoninaction/ch16.htm" onfocus="this.blur()">
        
        
        第十六章 在应用程序中加入HTML
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch17.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        " name="ch17.htm" href="http://www.czug.org/python/wxpythoninaction/ch17.htm" onfocus="this.blur()">
        
        
        第十七章 wxPython的打印构架
            </a>
    </div>
    
</li>

<li class="navTreeItem visualNoMarker node selected notloaded collapsed" kssattr:nodeurl="http://www.czug.org/python/wxpythoninaction/ch18.htm">
    <div class="visualIcon contenttype-folder">
        <a border="0" class="visualIconPadding 
        navTreeCurrentItem" name="ch18.htm" href="http://www.czug.org/python/wxpythoninaction/ch18.htm" onfocus="this.blur()">
        
        
        第十八章 使用wxPython的其他功能
            </a>
    </div>
    
</li>

            <!--p class="navTreeItem visualNoMarker" 
               tal:repeat="node items" 
               tal:replace="structure node">
            </p-->
        </ul>
</dd>
    <dd class="portletFooter">
        <div><!-- --></div>
    </dd>
  </dl>
<b class="xbottom"><b class="xb4"></b><b class="xb3"></b><b class="xb2"></b><b class="xb1"></b></b>
</div>
</div>
 </div> </td>
<td id="portal-column-content">
                            <div id="content" class="documentContent">
<h1>第十八章 使用wxPython的其他功能</h1>
<div lang="en" id="content" dir="ltr">
<span id="top" class="anchor"></span>
<span id="line-1" class="anchor"></span><p class="line867"><strong>18 使用<tt class="backtick">wxPython</tt>的其它功能</strong> <span id="line-2" class="anchor"></span><span id="line-3" class="anchor"></span></p>
<p class="line874">本章内容： <span id="line-4" class="anchor"></span><span id="line-5" class="anchor"></span></p>
<ul>
<li>放置对象到剪贴板上 <span id="line-6" class="anchor"></span>
</li>
<li>拖放 <span id="line-7" class="anchor"></span>
</li>
<li>传送和获取自定义对象 <span id="line-8" class="anchor"></span>
</li>
<li><p class="line862">使用<tt>wx.Timer</tt>设置定时的事件 <span id="line-9" class="anchor"></span></p></li>
<li><p class="line862">编写多线程的<tt>wxPython</tt>应用程序 <span id="line-10" class="anchor"></span><span id="line-11" class="anchor"></span><span id="line-12" class="anchor"></span><span id="line-13" class="anchor"></span></p></li>
</ul>
<p class="line867">
</p>
<h2 id="head-9e8df9e7d86316fc76d0c680497f69da1bc1bc58">1. 放置对象到剪贴板上</h2>
<span id="line-14" class="anchor"></span><span id="line-15" class="anchor"></span><span id="line-16" class="anchor"></span><p class="line862">在<tt>wxPython</tt>中，剪贴板和拖放特性是紧密相关的。期间，内部窗口的通信是由使用<tt>wx.DataObject</tt>类或它的子类的一个实例作为中介的。<tt>wx.DataObject</tt>是一个特殊的数据对象，它包含描述输出数据格式的元数据。我们将从剪贴板入手，然后我们将讨论拖放的不同处理。 <span id="line-17" class="anchor"></span><span id="line-18" class="anchor"></span></p>
<p class="line874">对于一个剪切和粘贴操作，有三个元素： <span id="line-19" class="anchor"></span><span id="line-20" class="anchor"></span></p>
<ul>
<li><p class="line891"><tt>source(</tt>源) <span id="line-21" class="anchor"></span></p></li>
<li><p class="line891"><tt>clipboard(</tt>剪贴板) <span id="line-22" class="anchor"></span></p></li>
<li><p class="line891"><tt>target(</tt>目标) <span id="line-23" class="anchor"></span><span id="line-24" class="anchor"></span></p></li>
</ul>
<p class="line862">如果<tt>source</tt>是在你的应用程序中，那么你的应用程序负责创建<tt>wx.DataObject</tt>的一个实例并把它放到剪贴板对象。通常<tt>source</tt>都是在你的应用程序的外部。 <span id="line-25" class="anchor"></span><span id="line-26" class="anchor"></span></p>
<p class="line862">这里的<tt>clipboard</tt>是一个全局对象，它容纳数据并在必要时与操作系统的剪贴板交互。 <span id="line-27" class="anchor"></span><span id="line-28" class="anchor"></span></p>
<p class="line867"><tt>target</tt>对象负责从剪贴板获取<tt>wx.DataObject</tt>并把它转换为对你的应用程序有用的那一类数据。 <span id="line-29" class="anchor"></span><span id="line-30" class="anchor"></span><span id="line-31" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-5edd0f15b2fd02dcefd85d8785196f908a7d2d59">1.1. 得到剪贴板中的数据</h3>
<span id="line-32" class="anchor"></span><span id="line-33" class="anchor"></span><span id="line-34" class="anchor"></span><p class="line862">如果你想你的应用程序能够引起一个剪贴事件，也就是说你想能够将数据剪切或复制到剪贴板，把数据放置到一个<tt>wx.DataObject</tt>里面。<tt>wx.DataObject</tt>知道自己能够被读写何种格式的数据。这点是比较重要的，例如如果你当时正在写一个词处理程序并希望给用户在粘贴时选择无格式文本的粘贴或丰富文本格式的粘贴的情况。然而大多数时候，在你的剪贴板行为中不需要太强大或太灵活的性能。对于最常用的情况，<tt>wxPython</tt>提供了三个预定义的<tt>wx.DataObject</tt>的子类：纯文本，位图图像和文件名。 <span id="line-35" class="anchor"></span><span id="line-36" class="anchor"></span></p>
<p class="line862">要传递纯文本，可以创建类<tt>wx.TextDataObject</tt>的一个实例，使用它如下的构造器： <span id="line-37" class="anchor"></span><span id="line-38" class="anchor"></span></p>
<p class="line867"><span id="line-39" class="anchor"></span><span id="line-40" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
function isnumbered(obj) {
return obj.childNodes.length && obj.firstChild.childNodes.length && obj.firstChild.firstChild.className == 'LineNumber';
}
function nformat(num,chrs,add) {
var nlen = Math.max(0,chrs-(''+num).length), res = '';
while (nlen>0) { res += ' '; nlen-- }
return res+num+add;
}
function addnumber(did, nstart, nstep) {
var c = document.getElementById(did), l = c.firstChild, n = 1;
if (!isnumbered(c))
if (typeof nstart == 'undefined') nstart = 1;
if (typeof nstep  == 'undefined') nstep = 1;
n = nstart;
while (l != null) {
if (l.tagName == 'SPAN') {
var s = document.createElement('SPAN');
s.className = 'LineNumber'
s.appendChild(document.createTextNode(nformat(n,4,' ')));
n += nstep;
if (l.childNodes.length)
l.insertBefore(s, l.firstChild)
else
l.appendChild(s)
}
l = l.nextSibling;
}
return false;
}
function remnumber(did) {
var c = document.getElementById(did), l = c.firstChild;
if (isnumbered(c))
while (l != null) {
if (l.tagName == 'SPAN' && l.firstChild.className == 'LineNumber') l.removeChild(l.firstChild);
l = l.nextSibling;
}
return false;
}
function togglenumber(did, nstart, nstep) {
var c = document.getElementById(did);
if (isnumbered(c)) {
remnumber(did);
} else {
addnumber(did,nstart,nstep);
}
return false;
}
</script><script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-23deef59ce28031a8c3791af71b529114db254d7_000\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-23deef59ce28031a8c3791af71b529114db254d7_000', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-23deef59ce28031a8c3791af71b529114db254d7_000" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">TextDataObject</span><span class="Operator">(</span><span class="ID">text</span><span class="Operator">=</span><span class="String">""</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-41" class="anchor"></span><span id="line-42" class="anchor"></span><p class="line862">参数<tt>text</tt>是你想传递到剪贴的文本。你可以使用<tt>Text(text)</tt>方法来设置该文本，你也可以使用<tt>GetText()</tt>方法来得到该文本，你还可以使用<tt>GetTextLength()</tt>方法来得到该文本的长度。 <span id="line-43" class="anchor"></span><span id="line-44" class="anchor"></span></p>
<p class="line862">一旦你创建了这种数据对象后，接着你必须访问剪贴板。系统的剪贴板在<tt>wxPython</tt>中是一个全局性的对象，名为<tt>wx.TheClipboard</tt>。要使用它，可以使用它的<tt>Open()</tt>方法来打开它。如果该剪贴板被打开了则该方法返回<tt>True</tt>，否则返回<tt>False</tt>。如果该剪贴板正在被另一应用程序写入的话，该剪贴板的打开有可能会失败，因此在使用该剪贴板之前，你应该检查打开方法的返回值。当你使用完剪贴板之后，你应该调用它的 <span id="line-45" class="anchor"></span><tt>Close()</tt>方法来关闭它。打开剪贴板会阻塞其它的剪贴板用户的使用，因此剪贴板打开的时间应该尽可能的短。 <span id="line-46" class="anchor"></span><span id="line-47" class="anchor"></span><span id="line-48" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-e0ff656761641951f6ffe7ccc058bb44f0cc3cc5">1.2. 处理剪贴板中的数据</h3>
<span id="line-49" class="anchor"></span><span id="line-50" class="anchor"></span><span id="line-51" class="anchor"></span><p class="line862">一旦你有了打开的剪贴板，你就可以处理它所包含的数据对象。你可以使用<tt>SetData(data)</tt>来将你的对象放置到剪贴板上，其中参数<tt>data</tt>是一个<tt>wx.DataObject</tt>实例。你可以使用方法<tt>Clear()</tt>方法来清空剪贴板。如果你希望在你的应用程序结束后，剪贴板上的数据还存在，那么你必须调用方法<tt>Flush()</tt>，该方法命令系统维持你的数据。否则，该<tt>wxPython</tt>剪贴板对象在你的应用程序退出时会被清除。 <span id="line-52" class="anchor"></span><span id="line-53" class="anchor"></span></p>
<p class="line874">下面是一段添加文本到剪贴板的代码： <span id="line-54" class="anchor"></span><span id="line-55" class="anchor"></span></p>
<p class="line867"><span id="line-56" class="anchor"></span><span id="line-57" class="anchor"></span><span id="line-58" class="anchor"></span><span id="line-59" class="anchor"></span><span id="line-60" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-bbc2be7466fec9cc016c12c1a03a36c5b898f69c_001\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-bbc2be7466fec9cc016c12c1a03a36c5b898f69c_001', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-bbc2be7466fec9cc016c12c1a03a36c5b898f69c_001" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">text_data</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TextDataObject</span><span class="Operator">(</span><span class="String">"hi there"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ResWord">if</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">Open</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   3 </span>    <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">SetData</span><span class="Operator">(</span><span class="ID">text_data</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   4 </span>    <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">Close</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-61" class="anchor"></span><span id="line-62" class="anchor"></span><span id="line-63" class="anchor"></span><p class="line867">
</p>
<h3 id="head-014f362af71dc328194aed3a4d49efc88f226aef">1.3. 获得剪贴板中的文本数据</h3>
<span id="line-64" class="anchor"></span><span id="line-65" class="anchor"></span><span id="line-66" class="anchor"></span><p class="line862">从剪贴板中获得文本数据也是很简单的。一旦你打开了剪贴板，你就可以调用<tt>GetData(data)</tt>方法，其中参数<tt>data</tt>是<tt>wx.DataObject</tt>的一些特定的子类的一个实例。如果剪贴板中的数据能够以与方法中的数据对象参数相一致的某种格式被输出的话，该方法的返回值则为<tt>True</tt>。这里，由于我们传递进的是一个<tt>wx.TextDataObject</tt>，那么返回值<tt>True</tt>就意味该剪贴板能够被转换到纯文本。下面是一段样例代码： <span id="line-67" class="anchor"></span><span id="line-68" class="anchor"></span></p>
<p class="line867"><span id="line-69" class="anchor"></span><span id="line-70" class="anchor"></span><span id="line-71" class="anchor"></span><span id="line-72" class="anchor"></span><span id="line-73" class="anchor"></span><span id="line-74" class="anchor"></span><span id="line-75" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-b8b9126b15523024f8a1ffaca1cb8dda16113043_002\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-b8b9126b15523024f8a1ffaca1cb8dda16113043_002', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-b8b9126b15523024f8a1ffaca1cb8dda16113043_002" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">text_data</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TextDataObject</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ResWord">if</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">Open</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   3 </span>    <span class="ID">success</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">GetData</span><span class="Operator">(</span><span class="ID">text_data</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   4 </span>    <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">Close</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   5 </span><span class="ResWord">if</span> <span class="ID">success</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   6 </span>    <span class="ResWord">return</span> <span class="ID">text_data</span><span class="Operator">.</span><span class="ID">GetText</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-76" class="anchor"></span><span id="line-77" class="anchor"></span><p class="line862">注意，当你从剪贴板获取数据时，数据并不关心是哪个应用程序将它放置到剪贴板的。剪贴板中的数据本身被底层的操作系统所管理，<tt>wxPython</tt>的责任是确保格式的匹配及你能够得到你能够处理的数据格式。 <span id="line-78" class="anchor"></span><span id="line-79" class="anchor"></span><span id="line-80" class="anchor"></span><span id="line-81" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-41253e643d6eca1e4f247e95a52f605eefec21e7">1.4. 实战剪贴板</h3>
<span id="line-82" class="anchor"></span><span id="line-83" class="anchor"></span><span id="line-84" class="anchor"></span><p class="line874">在这一节，我们将显示一个简单的例子，它演示了如何与剪贴板交换数据。它是一个有着两个按钮的框架，它使用户能够复制和粘贴文本。当你运行这个例子时，结果将会如图18.1所示。 <span id="line-85" class="anchor"></span><span id="line-86" class="anchor"></span><span id="line-87" class="anchor"></span></p>
<p class="line867"><strong>图18.1</strong> <span id="line-88" class="anchor"></span><span id="line-89" class="anchor"></span></p>
<p class="line867"><img title="attachment:w18.1.gif" src="/moin/WxPythonInAction/ChapterEighteen?action=AttachFile&amp;do=get&amp;target=w18.1.gif" class="attachment"><span id="line-90" class="anchor"></span><span id="line-91" class="anchor"></span><span id="line-92" class="anchor"></span></p>
<p class="line874">例18.1是产生图18.1的代码。 <span id="line-93" class="anchor"></span><span id="line-94" class="anchor"></span><span id="line-95" class="anchor"></span></p>
<p class="line867"><strong>例18.1</strong>  <span id="line-96" class="anchor"></span><strong>剪贴板交互示例</strong> <span id="line-97" class="anchor"></span><span id="line-98" class="anchor"></span></p>
<p class="line867"><span id="line-99" class="anchor"></span><span id="line-100" class="anchor"></span><span id="line-101" class="anchor"></span><span id="line-102" class="anchor"></span><span id="line-103" class="anchor"></span><span id="line-104" class="anchor"></span><span id="line-105" class="anchor"></span><span id="line-106" class="anchor"></span><span id="line-107" class="anchor"></span><span id="line-108" class="anchor"></span><span id="line-109" class="anchor"></span><span id="line-110" class="anchor"></span><span id="line-111" class="anchor"></span><span id="line-112" class="anchor"></span><span id="line-113" class="anchor"></span><span id="line-114" class="anchor"></span><span id="line-115" class="anchor"></span><span id="line-116" class="anchor"></span><span id="line-117" class="anchor"></span><span id="line-118" class="anchor"></span><span id="line-119" class="anchor"></span><span id="line-120" class="anchor"></span><span id="line-121" class="anchor"></span><span id="line-122" class="anchor"></span><span id="line-123" class="anchor"></span><span id="line-124" class="anchor"></span><span id="line-125" class="anchor"></span><span id="line-126" class="anchor"></span><span id="line-127" class="anchor"></span><span id="line-128" class="anchor"></span><span id="line-129" class="anchor"></span><span id="line-130" class="anchor"></span><span id="line-131" class="anchor"></span><span id="line-132" class="anchor"></span><span id="line-133" class="anchor"></span><span id="line-134" class="anchor"></span><span id="line-135" class="anchor"></span><span id="line-136" class="anchor"></span><span id="line-137" class="anchor"></span><span id="line-138" class="anchor"></span><span id="line-139" class="anchor"></span><span id="line-140" class="anchor"></span><span id="line-141" class="anchor"></span><span id="line-142" class="anchor"></span><span id="line-143" class="anchor"></span><span id="line-144" class="anchor"></span><span id="line-145" class="anchor"></span><span id="line-146" class="anchor"></span><span id="line-147" class="anchor"></span><span id="line-148" class="anchor"></span><span id="line-149" class="anchor"></span><span id="line-150" class="anchor"></span><span id="line-151" class="anchor"></span><span id="line-152" class="anchor"></span><span id="line-153" class="anchor"></span><span id="line-154" class="anchor"></span><span id="line-155" class="anchor"></span><span id="line-156" class="anchor"></span><span id="line-157" class="anchor"></span><span id="line-158" class="anchor"></span><span id="line-159" class="anchor"></span><span id="line-160" class="anchor"></span><span id="line-161" class="anchor"></span><span id="line-162" class="anchor"></span><span id="line-163" class="anchor"></span><span id="line-164" class="anchor"></span><span id="line-165" class="anchor"></span><span id="line-166" class="anchor"></span><span id="line-167" class="anchor"></span><span id="line-168" class="anchor"></span><span id="line-169" class="anchor"></span><span id="line-170" class="anchor"></span><span id="line-171" class="anchor"></span><span id="line-172" class="anchor"></span><span id="line-173" class="anchor"></span><span id="line-174" class="anchor"></span><span id="line-175" class="anchor"></span><span id="line-176" class="anchor"></span><span id="line-177" class="anchor"></span><span id="line-178" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-b9abd899a138807f1c38a313ea67e4ed23a9d626_003\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-b9abd899a138807f1c38a313ea67e4ed23a9d626_003', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-b9abd899a138807f1c38a313ea67e4ed23a9d626_003" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="Comment">#-*- encoding:UTF-8 -*-</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="Comment"></span><span class="ResWord">import</span> <span class="ID">wx</span></span>
<span class="line"><span class="LineNumber">   3 </span></span>
<span class="line"><span class="LineNumber">   4 </span><span class="ID">t1_text</span> <span class="Operator">=</span> <span class="String">"""\</span></span>
<span class="line"><span class="LineNumber">   5 </span><span class="String">The whole contents of this control</span></span>
<span class="line"><span class="LineNumber">   6 </span><span class="String">will be placed in the system's</span></span>
<span class="line"><span class="LineNumber">   7 </span><span class="String">clipboard when you click the copy</span></span>
<span class="line"><span class="LineNumber">   8 </span><span class="String">button below.</span></span>
<span class="line"><span class="LineNumber">   9 </span><span class="String">"""</span></span>
<span class="line"><span class="LineNumber">  10 </span></span>
<span class="line"><span class="LineNumber">  11 </span><span class="ID">t2_text</span> <span class="Operator">=</span> <span class="String">"""\</span></span>
<span class="line"><span class="LineNumber">  12 </span><span class="String">If the clipboard contains a text</span></span>
<span class="line"><span class="LineNumber">  13 </span><span class="String">data object then it will be placed</span></span>
<span class="line"><span class="LineNumber">  14 </span><span class="String">in this control when you click</span></span>
<span class="line"><span class="LineNumber">  15 </span><span class="String">the paste button below.  Try</span></span>
<span class="line"><span class="LineNumber">  16 </span><span class="String">copying to and pasting from</span></span>
<span class="line"><span class="LineNumber">  17 </span><span class="String">other applications too!</span></span>
<span class="line"><span class="LineNumber">  18 </span><span class="String">"""</span></span>
<span class="line"><span class="LineNumber">  19 </span></span>
<span class="line"><span class="LineNumber">  20 </span><span class="ResWord">class</span> <span class="ID">MyFrame</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  21 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  22 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">None</span><span class="Operator">,</span> <span class="ID">title</span><span class="Operator">=</span><span class="String">"Clipboard"</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  23 </span>                          <span class="ID">size</span><span class="Operator">=</span><span class="Operator">(</span><span class="Number">500</span><span class="Operator">,</span><span class="Number">300</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  24 </span>        <span class="ID">p</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Panel</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  25 </span></span>
<span class="line"><span class="LineNumber">  26 </span>        <span class="Comment"># create the controls</span></span>
<span class="line"><span class="LineNumber">  27 </span><span class="Comment"></span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">t1</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TextCtrl</span><span class="Operator">(</span><span class="ID">p</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="ID">t1_text</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  28 </span>                              <span class="ID">style</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">TE_MULTILINE</span><span class="Operator">|</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">HSCROLL</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  29 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">t2</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TextCtrl</span><span class="Operator">(</span><span class="ID">p</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="ID">t2_text</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  30 </span>                              <span class="ID">style</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">TE_MULTILINE</span><span class="Operator">|</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">HSCROLL</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  31 </span>        <span class="ID">copy</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Button</span><span class="Operator">(</span><span class="ID">p</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"Copy"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  32 </span>        <span class="ID">paste</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Button</span><span class="Operator">(</span><span class="ID">p</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"Paste"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  33 </span></span>
<span class="line"><span class="LineNumber">  34 </span>        <span class="Comment"># setup the layout with sizers</span></span>
<span class="line"><span class="LineNumber">  35 </span><span class="Comment"></span>        <span class="ID">fgs</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">FlexGridSizer</span><span class="Operator">(</span><span class="Number">2</span><span class="Operator">,</span> <span class="Number">2</span><span class="Operator">,</span> <span class="Number">5</span><span class="Operator">,</span> <span class="Number">5</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  36 </span>        <span class="ID">fgs</span><span class="Operator">.</span><span class="ID">AddGrowableRow</span><span class="Operator">(</span><span class="Number">0</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  37 </span>        <span class="ID">fgs</span><span class="Operator">.</span><span class="ID">AddGrowableCol</span><span class="Operator">(</span><span class="Number">0</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  38 </span>        <span class="ID">fgs</span><span class="Operator">.</span><span class="ID">AddGrowableCol</span><span class="Operator">(</span><span class="Number">1</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  39 </span>        <span class="ID">fgs</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">t1</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">EXPAND</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  40 </span>        <span class="ID">fgs</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">t2</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">EXPAND</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  41 </span>        <span class="ID">fgs</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">copy</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">EXPAND</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  42 </span>        <span class="ID">fgs</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">paste</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">EXPAND</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  43 </span>        <span class="ID">border</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BoxSizer</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  44 </span>        <span class="ID">border</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">fgs</span><span class="Operator">,</span> <span class="Number">1</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">EXPAND</span><span class="Operator">|</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">ALL</span><span class="Operator">,</span> <span class="Number">5</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  45 </span>        <span class="ID">p</span><span class="Operator">.</span><span class="ID">SetSizer</span><span class="Operator">(</span><span class="ID">border</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  46 </span></span>
<span class="line"><span class="LineNumber">  47 </span>        <span class="Comment"># Bind events</span></span>
<span class="line"><span class="LineNumber">  48 </span><span class="Comment"></span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_BUTTON</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnDoCopy</span><span class="Operator">,</span> <span class="ID">copy</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  49 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_BUTTON</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnDoPaste</span><span class="Operator">,</span> <span class="ID">paste</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  50 </span></span>
<span class="line"><span class="LineNumber">  51 </span>    <span class="ResWord">def</span> <span class="ID">OnDoCopy</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span><span class="Comment">#Copy按钮的事件处理函数</span></span>
<span class="line"><span class="LineNumber">  52 </span>        <span class="ID">data</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TextDataObject</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  53 </span>        <span class="ID">data</span><span class="Operator">.</span><span class="ID">SetText</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">t1</span><span class="Operator">.</span><span class="ID">GetValue</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  54 </span>        <span class="ResWord">if</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">Open</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  55 </span>            <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">SetData</span><span class="Operator">(</span><span class="ID">data</span><span class="Operator">)</span><span class="Comment">#将数据放置到剪贴板上</span></span>
<span class="line"><span class="LineNumber">  56 </span>            <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">Close</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  57 </span>        <span class="ResWord">else</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  58 </span>            <span class="ID">wx</span><span class="Operator">.</span><span class="ID">MessageBox</span><span class="Operator">(</span><span class="String">"Unable to open the clipboard"</span><span class="Operator">,</span> <span class="String">"Error"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  59 </span></span>
<span class="line"><span class="LineNumber">  60 </span>    <span class="ResWord">def</span> <span class="ID">OnDoPaste</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span><span class="Comment">#Paste按钮的事件处理函数</span></span>
<span class="line"><span class="LineNumber">  61 </span>        <span class="ID">success</span> <span class="Operator">=</span> <span class="ID">False</span></span>
<span class="line"><span class="LineNumber">  62 </span>        <span class="ID">data</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TextDataObject</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  63 </span>        <span class="ResWord">if</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">Open</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  64 </span>            <span class="ID">success</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">GetData</span><span class="Operator">(</span><span class="ID">data</span><span class="Operator">)</span><span class="Comment">#从剪贴板得到数据</span></span>
<span class="line"><span class="LineNumber">  65 </span>            <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">Close</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  66 </span></span>
<span class="line"><span class="LineNumber">  67 </span>        <span class="ResWord">if</span> <span class="ID">success</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  68 </span>            <span class="ID">self</span><span class="Operator">.</span><span class="ID">t2</span><span class="Operator">.</span><span class="ID">SetValue</span><span class="Operator">(</span><span class="ID">data</span><span class="Operator">.</span><span class="ID">GetText</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">)</span><span class="Comment">#更新文本控件</span></span>
<span class="line"><span class="LineNumber">  69 </span>        <span class="ResWord">else</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  70 </span>            <span class="ID">wx</span><span class="Operator">.</span><span class="ID">MessageBox</span><span class="Operator">(</span></span>
<span class="line"><span class="LineNumber">  71 </span>                <span class="String">"There is no data in the clipboard in the required format"</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  72 </span>                <span class="String">"Error"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  73 </span></span>
<span class="line"><span class="LineNumber">  74 </span></span>
<span class="line"><span class="LineNumber">  75 </span><span class="ID">app</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">PySimpleApp</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  76 </span><span class="ID">frm</span> <span class="Operator">=</span> <span class="ID">MyFrame</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  77 </span><span class="ID">frm</span><span class="Operator">.</span><span class="ID">Show</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  78 </span><span class="ID">app</span><span class="Operator">.</span><span class="ID">MainLoop</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-179" class="anchor"></span><span id="line-180" class="anchor"></span><span id="line-181" class="anchor"></span><span id="line-182" class="anchor"></span><p class="line867">
</p>
<h3 id="head-014f362af71dc328194aed3a4d49efc88f226aef-2">1.5. 获得剪贴板中的文本数据</h3>
<span id="line-183" class="anchor"></span><span id="line-184" class="anchor"></span><span id="line-185" class="anchor"></span><p class="line862">从剪贴板中获得文本数据也是很简单的。一旦你打开了剪贴板，你就可以调用<tt>GetData(data)</tt>方法，其中参数<tt>data</tt>是<tt>wx.DataObject</tt>的一些特定的子类的一个实例。如果剪贴板中的数据能够以与方法中的数据对象参数相一致的某种格式被输出的话，该方法的返回值则为<tt>True</tt>。这里，由于我们传递进的是一个<tt>wx.TextDataObject</tt>，那么返回值<tt>True</tt>就意味该剪贴板能够被转换到纯文本。下面是一段样例代码： <span id="line-186" class="anchor"></span><span id="line-187" class="anchor"></span></p>
<p class="line867"><span id="line-188" class="anchor"></span><span id="line-189" class="anchor"></span><span id="line-190" class="anchor"></span><span id="line-191" class="anchor"></span><span id="line-192" class="anchor"></span><span id="line-193" class="anchor"></span><span id="line-194" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-b8b9126b15523024f8a1ffaca1cb8dda16113043_004\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-b8b9126b15523024f8a1ffaca1cb8dda16113043_004', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-b8b9126b15523024f8a1ffaca1cb8dda16113043_004" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">text_data</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TextDataObject</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ResWord">if</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">Open</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   3 </span>    <span class="ID">success</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">GetData</span><span class="Operator">(</span><span class="ID">text_data</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   4 </span>    <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">Close</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   5 </span><span class="ResWord">if</span> <span class="ID">success</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   6 </span>    <span class="ResWord">return</span> <span class="ID">text_data</span><span class="Operator">.</span><span class="ID">GetText</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-195" class="anchor"></span><span id="line-196" class="anchor"></span><p class="line862">注意，当你从剪贴板获取数据时，数据并不关心是哪个应用程序将它放置到剪贴板的。剪贴板中的数据本身被底层的操作系统所管理，<tt>wxPython</tt>的责任是确保格式的匹配及你能够得到你能够处理的数据格式。 <span id="line-197" class="anchor"></span><span id="line-198" class="anchor"></span><span id="line-199" class="anchor"></span><span id="line-200" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-41253e643d6eca1e4f247e95a52f605eefec21e7-2">1.6. 实战剪贴板</h3>
<span id="line-201" class="anchor"></span><span id="line-202" class="anchor"></span><span id="line-203" class="anchor"></span><p class="line874">在这一节，我们将显示一个简单的例子，它演示了如何与剪贴板交换数据。它是一个有着两个按钮的框架，它使用户能够复制和粘贴文本。当你运行这个例子时，结果将会如图18.1所示。 <span id="line-204" class="anchor"></span><span id="line-205" class="anchor"></span><span id="line-206" class="anchor"></span></p>
<p class="line867"><strong>图18.1</strong> <span id="line-207" class="anchor"></span><span id="line-208" class="anchor"></span></p>
<p class="line867"><img title="attachment:w18.1.gif" src="/moin/WxPythonInAction/ChapterEighteen?action=AttachFile&amp;do=get&amp;target=w18.1.gif" class="attachment"><span id="line-209" class="anchor"></span><span id="line-210" class="anchor"></span><span id="line-211" class="anchor"></span></p>
<p class="line874">例18.1是产生图18.1的代码。 <span id="line-212" class="anchor"></span><span id="line-213" class="anchor"></span><span id="line-214" class="anchor"></span></p>
<p class="line867"><strong>例18.1</strong>  <span id="line-215" class="anchor"></span><strong>剪贴板交互示例</strong> <span id="line-216" class="anchor"></span><span id="line-217" class="anchor"></span></p>
<p class="line867"><span id="line-218" class="anchor"></span><span id="line-219" class="anchor"></span><span id="line-220" class="anchor"></span><span id="line-221" class="anchor"></span><span id="line-222" class="anchor"></span><span id="line-223" class="anchor"></span><span id="line-224" class="anchor"></span><span id="line-225" class="anchor"></span><span id="line-226" class="anchor"></span><span id="line-227" class="anchor"></span><span id="line-228" class="anchor"></span><span id="line-229" class="anchor"></span><span id="line-230" class="anchor"></span><span id="line-231" class="anchor"></span><span id="line-232" class="anchor"></span><span id="line-233" class="anchor"></span><span id="line-234" class="anchor"></span><span id="line-235" class="anchor"></span><span id="line-236" class="anchor"></span><span id="line-237" class="anchor"></span><span id="line-238" class="anchor"></span><span id="line-239" class="anchor"></span><span id="line-240" class="anchor"></span><span id="line-241" class="anchor"></span><span id="line-242" class="anchor"></span><span id="line-243" class="anchor"></span><span id="line-244" class="anchor"></span><span id="line-245" class="anchor"></span><span id="line-246" class="anchor"></span><span id="line-247" class="anchor"></span><span id="line-248" class="anchor"></span><span id="line-249" class="anchor"></span><span id="line-250" class="anchor"></span><span id="line-251" class="anchor"></span><span id="line-252" class="anchor"></span><span id="line-253" class="anchor"></span><span id="line-254" class="anchor"></span><span id="line-255" class="anchor"></span><span id="line-256" class="anchor"></span><span id="line-257" class="anchor"></span><span id="line-258" class="anchor"></span><span id="line-259" class="anchor"></span><span id="line-260" class="anchor"></span><span id="line-261" class="anchor"></span><span id="line-262" class="anchor"></span><span id="line-263" class="anchor"></span><span id="line-264" class="anchor"></span><span id="line-265" class="anchor"></span><span id="line-266" class="anchor"></span><span id="line-267" class="anchor"></span><span id="line-268" class="anchor"></span><span id="line-269" class="anchor"></span><span id="line-270" class="anchor"></span><span id="line-271" class="anchor"></span><span id="line-272" class="anchor"></span><span id="line-273" class="anchor"></span><span id="line-274" class="anchor"></span><span id="line-275" class="anchor"></span><span id="line-276" class="anchor"></span><span id="line-277" class="anchor"></span><span id="line-278" class="anchor"></span><span id="line-279" class="anchor"></span><span id="line-280" class="anchor"></span><span id="line-281" class="anchor"></span><span id="line-282" class="anchor"></span><span id="line-283" class="anchor"></span><span id="line-284" class="anchor"></span><span id="line-285" class="anchor"></span><span id="line-286" class="anchor"></span><span id="line-287" class="anchor"></span><span id="line-288" class="anchor"></span><span id="line-289" class="anchor"></span><span id="line-290" class="anchor"></span><span id="line-291" class="anchor"></span><span id="line-292" class="anchor"></span><span id="line-293" class="anchor"></span><span id="line-294" class="anchor"></span><span id="line-295" class="anchor"></span><span id="line-296" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-b9abd899a138807f1c38a313ea67e4ed23a9d626_005\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-b9abd899a138807f1c38a313ea67e4ed23a9d626_005', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-b9abd899a138807f1c38a313ea67e4ed23a9d626_005" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="Comment">#-*- encoding:UTF-8 -*-</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="Comment"></span><span class="ResWord">import</span> <span class="ID">wx</span></span>
<span class="line"><span class="LineNumber">   3 </span></span>
<span class="line"><span class="LineNumber">   4 </span><span class="ID">t1_text</span> <span class="Operator">=</span> <span class="String">"""\</span></span>
<span class="line"><span class="LineNumber">   5 </span><span class="String">The whole contents of this control</span></span>
<span class="line"><span class="LineNumber">   6 </span><span class="String">will be placed in the system's</span></span>
<span class="line"><span class="LineNumber">   7 </span><span class="String">clipboard when you click the copy</span></span>
<span class="line"><span class="LineNumber">   8 </span><span class="String">button below.</span></span>
<span class="line"><span class="LineNumber">   9 </span><span class="String">"""</span></span>
<span class="line"><span class="LineNumber">  10 </span></span>
<span class="line"><span class="LineNumber">  11 </span><span class="ID">t2_text</span> <span class="Operator">=</span> <span class="String">"""\</span></span>
<span class="line"><span class="LineNumber">  12 </span><span class="String">If the clipboard contains a text</span></span>
<span class="line"><span class="LineNumber">  13 </span><span class="String">data object then it will be placed</span></span>
<span class="line"><span class="LineNumber">  14 </span><span class="String">in this control when you click</span></span>
<span class="line"><span class="LineNumber">  15 </span><span class="String">the paste button below.  Try</span></span>
<span class="line"><span class="LineNumber">  16 </span><span class="String">copying to and pasting from</span></span>
<span class="line"><span class="LineNumber">  17 </span><span class="String">other applications too!</span></span>
<span class="line"><span class="LineNumber">  18 </span><span class="String">"""</span></span>
<span class="line"><span class="LineNumber">  19 </span></span>
<span class="line"><span class="LineNumber">  20 </span><span class="ResWord">class</span> <span class="ID">MyFrame</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  21 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  22 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">None</span><span class="Operator">,</span> <span class="ID">title</span><span class="Operator">=</span><span class="String">"Clipboard"</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  23 </span>                          <span class="ID">size</span><span class="Operator">=</span><span class="Operator">(</span><span class="Number">500</span><span class="Operator">,</span><span class="Number">300</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  24 </span>        <span class="ID">p</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Panel</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  25 </span></span>
<span class="line"><span class="LineNumber">  26 </span>        <span class="Comment"># create the controls</span></span>
<span class="line"><span class="LineNumber">  27 </span><span class="Comment"></span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">t1</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TextCtrl</span><span class="Operator">(</span><span class="ID">p</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="ID">t1_text</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  28 </span>                              <span class="ID">style</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">TE_MULTILINE</span><span class="Operator">|</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">HSCROLL</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  29 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">t2</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TextCtrl</span><span class="Operator">(</span><span class="ID">p</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="ID">t2_text</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  30 </span>                              <span class="ID">style</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">TE_MULTILINE</span><span class="Operator">|</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">HSCROLL</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  31 </span>        <span class="ID">copy</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Button</span><span class="Operator">(</span><span class="ID">p</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"Copy"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  32 </span>        <span class="ID">paste</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Button</span><span class="Operator">(</span><span class="ID">p</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"Paste"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  33 </span></span>
<span class="line"><span class="LineNumber">  34 </span>        <span class="Comment"># setup the layout with sizers</span></span>
<span class="line"><span class="LineNumber">  35 </span><span class="Comment"></span>        <span class="ID">fgs</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">FlexGridSizer</span><span class="Operator">(</span><span class="Number">2</span><span class="Operator">,</span> <span class="Number">2</span><span class="Operator">,</span> <span class="Number">5</span><span class="Operator">,</span> <span class="Number">5</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  36 </span>        <span class="ID">fgs</span><span class="Operator">.</span><span class="ID">AddGrowableRow</span><span class="Operator">(</span><span class="Number">0</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  37 </span>        <span class="ID">fgs</span><span class="Operator">.</span><span class="ID">AddGrowableCol</span><span class="Operator">(</span><span class="Number">0</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  38 </span>        <span class="ID">fgs</span><span class="Operator">.</span><span class="ID">AddGrowableCol</span><span class="Operator">(</span><span class="Number">1</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  39 </span>        <span class="ID">fgs</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">t1</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">EXPAND</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  40 </span>        <span class="ID">fgs</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">t2</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">EXPAND</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  41 </span>        <span class="ID">fgs</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">copy</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">EXPAND</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  42 </span>        <span class="ID">fgs</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">paste</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">EXPAND</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  43 </span>        <span class="ID">border</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BoxSizer</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  44 </span>        <span class="ID">border</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">fgs</span><span class="Operator">,</span> <span class="Number">1</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">EXPAND</span><span class="Operator">|</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">ALL</span><span class="Operator">,</span> <span class="Number">5</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  45 </span>        <span class="ID">p</span><span class="Operator">.</span><span class="ID">SetSizer</span><span class="Operator">(</span><span class="ID">border</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  46 </span></span>
<span class="line"><span class="LineNumber">  47 </span>        <span class="Comment"># Bind events</span></span>
<span class="line"><span class="LineNumber">  48 </span><span class="Comment"></span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_BUTTON</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnDoCopy</span><span class="Operator">,</span> <span class="ID">copy</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  49 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_BUTTON</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnDoPaste</span><span class="Operator">,</span> <span class="ID">paste</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  50 </span></span>
<span class="line"><span class="LineNumber">  51 </span>    <span class="ResWord">def</span> <span class="ID">OnDoCopy</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span><span class="Comment">#Copy按钮的事件处理函数</span></span>
<span class="line"><span class="LineNumber">  52 </span>        <span class="ID">data</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TextDataObject</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  53 </span>        <span class="ID">data</span><span class="Operator">.</span><span class="ID">SetText</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">t1</span><span class="Operator">.</span><span class="ID">GetValue</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  54 </span>        <span class="ResWord">if</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">Open</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  55 </span>            <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">SetData</span><span class="Operator">(</span><span class="ID">data</span><span class="Operator">)</span><span class="Comment">#将数据放置到剪贴板上</span></span>
<span class="line"><span class="LineNumber">  56 </span>            <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">Close</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  57 </span>        <span class="ResWord">else</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  58 </span>            <span class="ID">wx</span><span class="Operator">.</span><span class="ID">MessageBox</span><span class="Operator">(</span><span class="String">"Unable to open the clipboard"</span><span class="Operator">,</span> <span class="String">"Error"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  59 </span></span>
<span class="line"><span class="LineNumber">  60 </span>    <span class="ResWord">def</span> <span class="ID">OnDoPaste</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span><span class="Comment">#Paste按钮的事件处理函数</span></span>
<span class="line"><span class="LineNumber">  61 </span>        <span class="ID">success</span> <span class="Operator">=</span> <span class="ID">False</span></span>
<span class="line"><span class="LineNumber">  62 </span>        <span class="ID">data</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TextDataObject</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  63 </span>        <span class="ResWord">if</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">Open</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  64 </span>            <span class="ID">success</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">GetData</span><span class="Operator">(</span><span class="ID">data</span><span class="Operator">)</span><span class="Comment">#从剪贴板得到数据</span></span>
<span class="line"><span class="LineNumber">  65 </span>            <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">Close</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  66 </span></span>
<span class="line"><span class="LineNumber">  67 </span>        <span class="ResWord">if</span> <span class="ID">success</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  68 </span>            <span class="ID">self</span><span class="Operator">.</span><span class="ID">t2</span><span class="Operator">.</span><span class="ID">SetValue</span><span class="Operator">(</span><span class="ID">data</span><span class="Operator">.</span><span class="ID">GetText</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">)</span><span class="Comment">#更新文本控件</span></span>
<span class="line"><span class="LineNumber">  69 </span>        <span class="ResWord">else</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  70 </span>            <span class="ID">wx</span><span class="Operator">.</span><span class="ID">MessageBox</span><span class="Operator">(</span></span>
<span class="line"><span class="LineNumber">  71 </span>                <span class="String">"There is no data in the clipboard in the required format"</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  72 </span>                <span class="String">"Error"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  73 </span></span>
<span class="line"><span class="LineNumber">  74 </span></span>
<span class="line"><span class="LineNumber">  75 </span><span class="ID">app</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">PySimpleApp</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  76 </span><span class="ID">frm</span> <span class="Operator">=</span> <span class="ID">MyFrame</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  77 </span><span class="ID">frm</span><span class="Operator">.</span><span class="ID">Show</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  78 </span><span class="ID">app</span><span class="Operator">.</span><span class="ID">MainLoop</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-297" class="anchor"></span><span id="line-298" class="anchor"></span><p class="line874">在下一节中，我们将讨论如何传递其它格式的数据，如位图。 <span id="line-299" class="anchor"></span><span id="line-300" class="anchor"></span><span id="line-301" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-7fd263623c54b853369d0487fb93f509ce61414a">1.7. 传递其它格式的数据</h3>
<span id="line-302" class="anchor"></span><span id="line-303" class="anchor"></span><span id="line-304" class="anchor"></span><p class="line862">经由剪贴板交互位图几乎与传递文本相同。你所使用的相关的数据对象子类是<tt>wx.BitmapDataObject</tt>，其<tt>get</tt>*方法和<tt>set</tt>*方法分别是<tt>GetBitmap()</tt>和<tt>SetBitmap(bitmap)</tt>。经由该数据对象与剪贴板交互的数据对象必须是<tt>wx.Bitmap</tt>类型的。 <span id="line-305" class="anchor"></span><span id="line-306" class="anchor"></span></p>
<p class="line862">最后一个预定义的数据对象类型是<tt>wx.FileDataObject</tt>。通常该数据对象被用于拖放中（将在18.2节中讨论），例如当你将一个文件从你的资源管理器或查找窗口放置到你的应用程序上时。你可以使用该数据对象从剪贴板接受文件名数据，并且你可以使用方法<tt>GetFilenames()</tt>来从该数据对象获取文件名，该方法返回一个文件名的列表，列表中的每个文件名是已经被添加到剪贴板的文件名。你可以使用该数据对象的<tt>AddFile(file)</tt>方法来将数据放置到剪贴板上，该方法将一个文件名字符串添加到该数据对象。这里没有其它的方法用于直接处理列表，所以这就要靠你自己了。本章的稍后部份，我们将讨论如何经由剪贴板传送自定义对象，以及如何拖放对象。
<span id="line-307" class="anchor"></span><span id="line-308" class="anchor"></span><span id="line-309" class="anchor"></span></p>
<p class="line867">
</p>
<h2 id="head-cc7e2d8b5a7ec8979f37cca4632c1618186cd00a">2. 拖放源</h2>
<span id="line-310" class="anchor"></span><span id="line-311" class="anchor"></span><span id="line-312" class="anchor"></span><p class="line862">拖放是一个类似剪切和粘贴的功能。它是在你的应用程序的不同部分之间或两个不同的应用程序之间传送数据。由于管理数据和格式几乎是相同的，所以<tt>wxPython</tt>同样使用<tt>wx.DataObject</tt>族来确保对格式作恰当的处理。 <span id="line-313" class="anchor"></span><span id="line-314" class="anchor"></span></p>
<p class="line874">拖放和剪切粘贴的最大不同是，剪切粘贴信赖于中介剪贴板的存在。因为是剪贴板管理数据，所以源程序将数据传送后就不管之后的事情了。这对于拖放却不然，源应用程序不仅虽要创建一个拖动管理器来服务于剪贴板，而且它也必须等待目标应用程序的响应。不同于一个剪贴板的操作，在拖放中，是目标应用来决定操作是一个剪贴或拷贝，所以源应用必须等待以确定传送的数据所用的目的。 <span id="line-315" class="anchor"></span><span id="line-316" class="anchor"></span></p>
<p class="line874">通常，对源的拖动操作是在一个事件处理函数中进行，通常是一个鼠标事件，因为拖动通常都随鼠标的按下事件发生。创建一个拖动源要求四步： <span id="line-317" class="anchor"></span><span id="line-318" class="anchor"></span></p>
<p class="line874">1、创建数据对象 <span id="line-319" class="anchor"></span>2、创建<tt>wx.DropSource</tt>实例 <span id="line-320" class="anchor"></span>3、执行拖动操作 <span id="line-321" class="anchor"></span>4、取消或允许释放 <span id="line-322" class="anchor"></span><span id="line-323" class="anchor"></span><span id="line-324" class="anchor"></span></p>
<p class="line867"><strong>步骤1</strong>  <span id="line-325" class="anchor"></span><strong>创建一个数据对象</strong> <span id="line-326" class="anchor"></span><span id="line-327" class="anchor"></span></p>
<p class="line862">这第一步是创建你的数据对象。这在早先的剪贴板操作中有很好的说明。对于简单的数据，使用预定义的<tt>wx.DataObject</tt>的子类是最简单的。有了数据对象后，你可以创建一个释放源实例 <span id="line-328" class="anchor"></span><span id="line-329" class="anchor"></span><span id="line-330" class="anchor"></span></p>
<p class="line867"><strong>步骤2</strong>  <span id="line-331" class="anchor"></span><strong>创建释放源实例</strong> <span id="line-332" class="anchor"></span><span id="line-333" class="anchor"></span></p>
<p class="line862">接下来的步骤是创建一个<tt>wx.DropSource</tt>实例，它扮演类似于剪贴板这样的传送角色。<tt>wx.DropSource</tt>的构造函数如下： <span id="line-334" class="anchor"></span><span id="line-335" class="anchor"></span></p>
<p class="line867"><span id="line-336" class="anchor"></span><span id="line-337" class="anchor"></span><span id="line-338" class="anchor"></span><span id="line-339" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-20a2373e979255340814dbf4088a5391f1a794bb_006\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-20a2373e979255340814dbf4088a5391f1a794bb_006', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-20a2373e979255340814dbf4088a5391f1a794bb_006" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">DropSource</span><span class="Operator">(</span><span class="ID">win</span><span class="Operator">,</span> <span class="ID">iconCopy</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">NullIconOrCursor</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">   2 </span>        <span class="ID">iconMove</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">NullIconOrCursor</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">   3 </span>        <span class="ID">iconNone</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">NullIconOrCursor</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-340" class="anchor"></span><span id="line-341" class="anchor"></span><p class="line862">参数<tt>win</tt>是初始化拖放操作的窗口对象。其余的三个参数用于使用自定义的图片来代表鼠标的拖动意义（拷贝、移动、取消释放）。如果这三个参数没有指定，那么使用系统的默认值。在微软的<tt>Windows</tt>系统上，图片必须是<tt>wx.Cursor</tt>对象，对于<tt>Unix</tt>则应是<tt>wx.Icon</tt>对象——<tt>Mac OS</tt>目前忽略你的自定义图片。 <span id="line-342" class="anchor"></span><span id="line-343" class="anchor"></span></p>
<p class="line862">一旦你有了你的<tt>wx.DropSource</tt>实例，那么就可以使用方法<tt>SetData(data)</tt>来将你的数据对象关联到<tt>wx.DropSource</tt>实例。接下来我们将讨论实际的拖动。 <span id="line-344" class="anchor"></span><span id="line-345" class="anchor"></span><span id="line-346" class="anchor"></span></p>
<p class="line867"><strong>步骤3</strong>  <span id="line-347" class="anchor"></span><strong>执行拖动</strong> <span id="line-348" class="anchor"></span><span id="line-349" class="anchor"></span></p>
<p class="line862">拖动操作通过调用释放源的方法<tt>DoDragDrop(flags=wx.Drag_CopyOnly)</tt>来开始。参数<tt>flags</tt>表示目标可对数据执行的何种操作。取值有<tt>wx.Drag_AllowMove</tt>，它表示批准执行一个移动或拷贝，<tt>wx.Drag_DefaultMove</tt>表示不仅允许执行一个移动或拷贝，而且做默认的移动操作，<tt>wx.Drag_CopyOnly</tt>表示只执行一个拷贝操作。 <span id="line-350" class="anchor"></span><span id="line-351" class="anchor"></span><span id="line-352" class="anchor"></span></p>
<p class="line867"><strong>步骤4</strong>  <span id="line-353" class="anchor"></span><strong>处理释放</strong> <span id="line-354" class="anchor"></span><span id="line-355" class="anchor"></span></p>
<p class="line867"><tt>DoDragDrop()</tt>方法直到释放被目标取消或接受才会返回。在此期间，虽然绘制事件会继续被发送，但你的应用程序的线程被阻塞。<tt>DoDragDrop()</tt>的返回值基于目标所要求的操作，取值如下： <span id="line-356" class="anchor"></span><span id="line-357" class="anchor"></span></p>
<p class="line867"><tt>wx.DragCancel</tt>（对于取消操作而言）  <span id="line-358" class="anchor"></span><span id="line-359" class="anchor"></span></p>
<p class="line867"><tt>wx.DragCopy </tt>（对于拷贝操作而言） <span id="line-360" class="anchor"></span><span id="line-361" class="anchor"></span></p>
<p class="line867"><tt>wx.DragMove </tt>（对于移动操作而言） <span id="line-362" class="anchor"></span><span id="line-363" class="anchor"></span></p>
<p class="line867"><tt>wx.DragNone </tt>（对于错误而言） <span id="line-364" class="anchor"></span><span id="line-365" class="anchor"></span><span id="line-366" class="anchor"></span></p>
<p class="line874">对这些返回值的响应由你的应用程序来负责。通常对于响应移动要删除被拖动的数据外，对于拷贝则是什么也不用做。 <span id="line-367" class="anchor"></span><span id="line-368" class="anchor"></span><span id="line-369" class="anchor"></span><span id="line-370" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-44d07fa39edd9836e34b842fd9e993c89b546ea1">2.1. 实战拖动</h3>
<span id="line-371" class="anchor"></span><span id="line-372" class="anchor"></span><span id="line-373" class="anchor"></span><p class="line862">例18.2显示了一个完整的拖动源控件，适合于通过拖动上面的箭头图片到你的系统的任何接受文本的应用上（如<tt>Microsoft word</tt>）。图18.2图示了这个例子。 <span id="line-374" class="anchor"></span><span id="line-375" class="anchor"></span><span id="line-376" class="anchor"></span></p>
<p class="line867"><strong>图18.2</strong> <span id="line-377" class="anchor"></span><span id="line-378" class="anchor"></span></p>
<p class="line867"><img title="attachment:w18.2.gif" src="/moin/WxPythonInAction/ChapterEighteen?action=AttachFile&amp;do=get&amp;target=w18.2.gif" class="attachment"><span id="line-379" class="anchor"></span><span id="line-380" class="anchor"></span><span id="line-381" class="anchor"></span><span id="line-382" class="anchor"></span></p>
<p class="line867"><strong>例18.2</strong>  <span id="line-383" class="anchor"></span><strong>一个小的拖动源控件</strong> <span id="line-384" class="anchor"></span><span id="line-385" class="anchor"></span></p>
<p class="line867"><span id="line-386" class="anchor"></span><span id="line-387" class="anchor"></span><span id="line-388" class="anchor"></span><span id="line-389" class="anchor"></span><span id="line-390" class="anchor"></span><span id="line-391" class="anchor"></span><span id="line-392" class="anchor"></span><span id="line-393" class="anchor"></span><span id="line-394" class="anchor"></span><span id="line-395" class="anchor"></span><span id="line-396" class="anchor"></span><span id="line-397" class="anchor"></span><span id="line-398" class="anchor"></span><span id="line-399" class="anchor"></span><span id="line-400" class="anchor"></span><span id="line-401" class="anchor"></span><span id="line-402" class="anchor"></span><span id="line-403" class="anchor"></span><span id="line-404" class="anchor"></span><span id="line-405" class="anchor"></span><span id="line-406" class="anchor"></span><span id="line-407" class="anchor"></span><span id="line-408" class="anchor"></span><span id="line-409" class="anchor"></span><span id="line-410" class="anchor"></span><span id="line-411" class="anchor"></span><span id="line-412" class="anchor"></span><span id="line-413" class="anchor"></span><span id="line-414" class="anchor"></span><span id="line-415" class="anchor"></span><span id="line-416" class="anchor"></span><span id="line-417" class="anchor"></span><span id="line-418" class="anchor"></span><span id="line-419" class="anchor"></span><span id="line-420" class="anchor"></span><span id="line-421" class="anchor"></span><span id="line-422" class="anchor"></span><span id="line-423" class="anchor"></span><span id="line-424" class="anchor"></span><span id="line-425" class="anchor"></span><span id="line-426" class="anchor"></span><span id="line-427" class="anchor"></span><span id="line-428" class="anchor"></span><span id="line-429" class="anchor"></span><span id="line-430" class="anchor"></span><span id="line-431" class="anchor"></span><span id="line-432" class="anchor"></span><span id="line-433" class="anchor"></span><span id="line-434" class="anchor"></span><span id="line-435" class="anchor"></span><span id="line-436" class="anchor"></span><span id="line-437" class="anchor"></span><span id="line-438" class="anchor"></span><span id="line-439" class="anchor"></span><span id="line-440" class="anchor"></span><span id="line-441" class="anchor"></span><span id="line-442" class="anchor"></span><span id="line-443" class="anchor"></span><span id="line-444" class="anchor"></span><span id="line-445" class="anchor"></span><span id="line-446" class="anchor"></span><span id="line-447" class="anchor"></span><span id="line-448" class="anchor"></span><span id="line-449" class="anchor"></span><span id="line-450" class="anchor"></span><span id="line-451" class="anchor"></span><span id="line-452" class="anchor"></span><span id="line-453" class="anchor"></span><span id="line-454" class="anchor"></span><span id="line-455" class="anchor"></span><span id="line-456" class="anchor"></span><span id="line-457" class="anchor"></span><span id="line-458" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-3756e55b721d8f110aa58330b31b868c43f4839b_007\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-3756e55b721d8f110aa58330b31b868c43f4839b_007', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-3756e55b721d8f110aa58330b31b868c43f4839b_007" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="Comment">#-*- encoding:UTF-8 -*-</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="Comment"></span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="ResWord">import</span> <span class="ID">wx</span></span>
<span class="line"><span class="LineNumber">   4 </span></span>
<span class="line"><span class="LineNumber">   5 </span><span class="ResWord">class</span> <span class="ID">DragController</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Control</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   6 </span>    <span class="String">"""</span></span>
<span class="line"><span class="LineNumber">   7 </span><span class="String">    Just a little control to handle dragging the text from a text</span></span>
<span class="line"><span class="LineNumber">   8 </span><span class="String">    control.  We use a separate control so as to not interfere with</span></span>
<span class="line"><span class="LineNumber">   9 </span><span class="String">    the native drag-select functionality of the native text control.</span></span>
<span class="line"><span class="LineNumber">  10 </span><span class="String">    """</span></span>
<span class="line"><span class="LineNumber">  11 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">parent</span><span class="Operator">,</span> <span class="ID">source</span><span class="Operator">,</span> <span class="ID">size</span><span class="Operator">=</span><span class="Operator">(</span><span class="Number">25</span><span class="Operator">,</span><span class="Number">25</span><span class="Operator">)</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  12 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Control</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">parent</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="ID">size</span><span class="Operator">=</span><span class="ID">size</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  13 </span>                            <span class="ID">style</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">SIMPLE_BORDER</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  14 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">source</span> <span class="Operator">=</span> <span class="ID">source</span></span>
<span class="line"><span class="LineNumber">  15 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">SetMinSize</span><span class="Operator">(</span><span class="ID">size</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  16 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_PAINT</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnPaint</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  17 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_LEFT_DOWN</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnLeftDown</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  18 </span></span>
<span class="line"><span class="LineNumber">  19 </span>    <span class="ResWord">def</span> <span class="ID">OnPaint</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  20 </span>        <span class="Comment"># draw a simple arrow</span></span>
<span class="line"><span class="LineNumber">  21 </span><span class="Comment"></span>        <span class="ID">dc</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BufferedPaintDC</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  22 </span>        <span class="ID">dc</span><span class="Operator">.</span><span class="ID">SetBackground</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Brush</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">GetBackgroundColour</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  23 </span>        <span class="ID">dc</span><span class="Operator">.</span><span class="ID">Clear</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  24 </span>        <span class="ID">w</span><span class="Operator">,</span> <span class="ID">h</span> <span class="Operator">=</span> <span class="ID">dc</span><span class="Operator">.</span><span class="ID">GetSize</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  25 </span>        <span class="ID">y</span> <span class="Operator">=</span> <span class="ID">h</span><span class="Operator">/</span><span class="Number">2</span></span>
<span class="line"><span class="LineNumber">  26 </span>        <span class="ID">dc</span><span class="Operator">.</span><span class="ID">SetPen</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Pen</span><span class="Operator">(</span><span class="String">"dark blue"</span><span class="Operator">,</span> <span class="Number">2</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  27 </span>        <span class="ID">dc</span><span class="Operator">.</span><span class="ID">DrawLine</span><span class="Operator">(</span><span class="ID">w</span><span class="Operator">/</span><span class="Number">8</span><span class="Operator">,</span>   <span class="ID">y</span><span class="Operator">,</span>  <span class="ID">w</span><span class="Operator">-</span><span class="ID">w</span><span class="Operator">/</span><span class="Number">8</span><span class="Operator">,</span> <span class="ID">y</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  28 </span>        <span class="ID">dc</span><span class="Operator">.</span><span class="ID">DrawLine</span><span class="Operator">(</span><span class="ID">w</span><span class="Operator">-</span><span class="ID">w</span><span class="Operator">/</span><span class="Number">8</span><span class="Operator">,</span> <span class="ID">y</span><span class="Operator">,</span>  <span class="ID">w</span><span class="Operator">/</span><span class="Number">2</span><span class="Operator">,</span>   <span class="ID">h</span><span class="Operator">/</span><span class="Number">4</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  29 </span>        <span class="ID">dc</span><span class="Operator">.</span><span class="ID">DrawLine</span><span class="Operator">(</span><span class="ID">w</span><span class="Operator">-</span><span class="ID">w</span><span class="Operator">/</span><span class="Number">8</span><span class="Operator">,</span> <span class="ID">y</span><span class="Operator">,</span>  <span class="ID">w</span><span class="Operator">/</span><span class="Number">2</span><span class="Operator">,</span>   <span class="Number">3</span><span class="Operator">*</span><span class="ID">h</span><span class="Operator">/</span><span class="Number">4</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  30 </span></span>
<span class="line"><span class="LineNumber">  31 </span>    <span class="ResWord">def</span> <span class="ID">OnLeftDown</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  32 </span>        <span class="ID">text</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">source</span><span class="Operator">.</span><span class="ID">GetValue</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  33 </span>        <span class="ID">data</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TextDataObject</span><span class="Operator">(</span><span class="ID">text</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  34 </span>        <span class="ID">dropSource</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">DropSource</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Comment">#创建释放源</span></span>
<span class="line"><span class="LineNumber">  35 </span>        <span class="ID">dropSource</span><span class="Operator">.</span><span class="ID">SetData</span><span class="Operator">(</span><span class="ID">data</span><span class="Operator">)</span><span class="Comment">#设置数据</span></span>
<span class="line"><span class="LineNumber">  36 </span>        <span class="ID">result</span> <span class="Operator">=</span> <span class="ID">dropSource</span><span class="Operator">.</span><span class="ID">DoDragDrop</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Drag_AllowMove</span><span class="Operator">)</span><span class="Comment">#执行释放</span></span>
<span class="line"><span class="LineNumber">  37 </span></span>
<span class="line"><span class="LineNumber">  38 </span>        <span class="Comment"># if the user wants to move the data then we should delete it</span></span>
<span class="line"><span class="LineNumber">  39 </span><span class="Comment"></span>        <span class="Comment"># from the source</span></span>
<span class="line"><span class="LineNumber">  40 </span><span class="Comment"></span>        <span class="ResWord">if</span> <span class="ID">result</span> <span class="Operator">==</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">DragMove</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  41 </span>            <span class="ID">self</span><span class="Operator">.</span><span class="ID">source</span><span class="Operator">.</span><span class="ID">SetValue</span><span class="Operator">(</span><span class="String">""</span><span class="Operator">)</span><span class="Comment">#如果需要的话，删除源中的数据</span></span>
<span class="line"><span class="LineNumber">  42 </span></span>
<span class="line"><span class="LineNumber">  43 </span><span class="ResWord">class</span> <span class="ID">MyFrame</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  44 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  45 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">None</span><span class="Operator">,</span> <span class="ID">title</span><span class="Operator">=</span><span class="String">"Drop Source"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  46 </span>        <span class="ID">p</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Panel</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  47 </span></span>
<span class="line"><span class="LineNumber">  48 </span>        <span class="Comment"># create the controls</span></span>
<span class="line"><span class="LineNumber">  49 </span><span class="Comment"></span>        <span class="ID">label1</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">StaticText</span><span class="Operator">(</span><span class="ID">p</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"Put some text in this control:"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  50 </span>        <span class="ID">label2</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">StaticText</span><span class="Operator">(</span><span class="ID">p</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  51 </span>           <span class="String">"Then drag from the neighboring bitmap and\n"</span></span>
<span class="line"><span class="LineNumber">  52 </span>           <span class="String">"drop in an application that accepts dropped\n"</span></span>
<span class="line"><span class="LineNumber">  53 </span>           <span class="String">"text, such as MS Word."</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  54 </span>        <span class="ID">text</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TextCtrl</span><span class="Operator">(</span><span class="ID">p</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"Some text"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  55 </span>        <span class="ID">dragctl</span> <span class="Operator">=</span> <span class="ID">DragController</span><span class="Operator">(</span><span class="ID">p</span><span class="Operator">,</span> <span class="ID">text</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  56 </span></span>
<span class="line"><span class="LineNumber">  57 </span>        <span class="Comment"># setup the layout with sizers</span></span>
<span class="line"><span class="LineNumber">  58 </span><span class="Comment"></span>        <span class="ID">sizer</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BoxSizer</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">VERTICAL</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  59 </span>        <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">label1</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ALL</span><span class="Operator">,</span> <span class="Number">5</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  60 </span>        <span class="ID">hrow</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BoxSizer</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">HORIZONTAL</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  61 </span>        <span class="ID">hrow</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">text</span><span class="Operator">,</span> <span class="Number">1</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">RIGHT</span><span class="Operator">,</span> <span class="Number">5</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  62 </span>        <span class="ID">hrow</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">dragctl</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  63 </span>        <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">hrow</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">EXPAND</span><span class="Operator">|</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">ALL</span><span class="Operator">,</span> <span class="Number">5</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  64 </span>        <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">label2</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ALL</span><span class="Operator">,</span> <span class="Number">5</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  65 </span>        <span class="ID">p</span><span class="Operator">.</span><span class="ID">SetSizer</span><span class="Operator">(</span><span class="ID">sizer</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  66 </span>        <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Fit</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  67 </span></span>
<span class="line"><span class="LineNumber">  68 </span></span>
<span class="line"><span class="LineNumber">  69 </span><span class="ID">app</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">PySimpleApp</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  70 </span><span class="ID">frm</span> <span class="Operator">=</span> <span class="ID">MyFrame</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  71 </span><span class="ID">frm</span><span class="Operator">.</span><span class="ID">Show</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  72 </span><span class="ID">app</span><span class="Operator">.</span><span class="ID">MainLoop</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-459" class="anchor"></span><span id="line-460" class="anchor"></span><p class="line874">接下来，我们将给你展示目标处的拖放。 <span id="line-461" class="anchor"></span><span id="line-462" class="anchor"></span><span id="line-463" class="anchor"></span></p>
<p class="line867">
</p>
<h2 id="head-359d6a881dfde8847a74ad9d06cf00fe7ae49cdd">3. 拖放到的目标</h2>
<span id="line-464" class="anchor"></span><span id="line-465" class="anchor"></span><span id="line-466" class="anchor"></span><p class="line862">实现拖放到的目标的步骤基本上借鉴了实现拖放源的步骤。其中最大的区别是，实现拖放源，你可以直接使用类<tt>wx.DropSource</tt>，而对于目标，你首先必须写你的自定义的<tt>wx.DropTarget</tt>的子类。一旦你有了你的目标类，你将需要创建它的一个实例，并通过使用<tt>wx.Window</tt>的<tt>SetDropTarget(target)</tt>方法将该实例与任一 <span id="line-467" class="anchor"></span><tt>wx.Window</tt>的实例关联起来。设置了目标后，<tt>wx.Window</tt>的实例（不论它是一个窗口，一个按钮，一个文本域或其它的控件）就变成了一个有效的释放目标。为了在你的释放目标上接受数据，你也必须创建一个所需要类型的<tt>wx.DataObject</tt>对象，并使用释放目标方法<tt>SetDataObject(data)</tt>将<tt>wx.DataObject</tt>对象与释放目标关联起来。在实际释放操作前，你需要预先定义数据对象，以便该释放目标能够正确地处理格式。要从目标获取该数据对象，有一个方法<tt>GetDataObject()</tt>。下面的样板代码使得释放目标能够接受文本（仅能接受文本）。这是因为数据对象已经被设置为<tt>wx.TextDataObject</tt>的一个实例。
<span id="line-468" class="anchor"></span><span id="line-469" class="anchor"></span></p>
<p class="line867"><span id="line-470" class="anchor"></span><span id="line-471" class="anchor"></span><span id="line-472" class="anchor"></span><span id="line-473" class="anchor"></span><span id="line-474" class="anchor"></span><span id="line-475" class="anchor"></span><span id="line-476" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-e2290df2798e936714c685e02b2e147e535a0381_008\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-e2290df2798e936714c685e02b2e147e535a0381_008', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-e2290df2798e936714c685e02b2e147e535a0381_008" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ResWord">class</span> <span class="ID">MyDropTarget</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">DropTarget</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   2 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   3 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">data</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TextDataObject</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   4 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">SetDataObject</span><span class="Operator">(</span><span class="ID">data</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   5 </span><span class="ID">target</span> <span class="Operator">=</span> <span class="ID">MyDataTarget</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   6 </span><span class="ID">win</span><span class="Operator">.</span><span class="ID">SetDropTarget</span><span class="Operator">(</span><span class="ID">target</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-477" class="anchor"></span><span id="line-478" class="anchor"></span><span id="line-479" class="anchor"></span><p class="line867">
</p>
<h3 id="head-9d3a0235963b10556ff4032ebb74c8a13a310c9a">3.1. 使用你的释放到的目标</h3>
<span id="line-480" class="anchor"></span><span id="line-481" class="anchor"></span><span id="line-482" class="anchor"></span><p class="line862">当一个释放发生时，你的<tt>wx.DropTarget</tt>子类的各种事件函数将被调用。其中最重要的是<tt>OnData(x, y,
default)</tt>，它是你必须在你自定义的释放目标类中覆盖的一个事件方法。参数<tt>x,y</tt>是释放时鼠标的位置。<tt>default</tt>参数是<tt>DoDragDrop()</tt>的四个取值之一，具体的值基于操作系统，传递给<tt>DoDragDrop()</tt>标志和当释放发生时修饰键的状态。在且仅在<tt>OnData()</tt>方法中，你可以调用<tt>GetData()</tt>。<tt>GetData()</tt>方法要求来自释放源的实际的数据并把它放入与你的释放目标对象相关联的数据对象中。<tt>GetData()</tt>不返回数据对象，所以你通常应该用一个实例变量来包含你的数据对象。下面是关于<tt>MyDropTarget.OnData()</tt>的样板代码：
<span id="line-483" class="anchor"></span><span id="line-484" class="anchor"></span></p>
<p class="line867"><span id="line-485" class="anchor"></span><span id="line-486" class="anchor"></span><span id="line-487" class="anchor"></span><span id="line-488" class="anchor"></span><span id="line-489" class="anchor"></span><span id="line-490" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-22609a9e42aca2862690a2267deb5b9f6d3b74ff_009\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-22609a9e42aca2862690a2267deb5b9f6d3b74ff_009', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-22609a9e42aca2862690a2267deb5b9f6d3b74ff_009" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ResWord">def</span> <span class="ID">OnData</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">x</span><span class="Operator">,</span> <span class="ID">y</span><span class="Operator">,</span> <span class="ID">default</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   2 </span>    <span class="ID">self</span><span class="Operator">.</span><span class="ID">GetData</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   3 </span>    <span class="ID">actual_data</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">data</span><span class="Operator">.</span><span class="ID">GetText</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   4 </span>    <span class="Comment"># Do something with the data here... </span></span>
<span class="line"><span class="LineNumber">   5 </span><span class="Comment"></span>    <span class="ResWord">return</span> <span class="ID">default</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-491" class="anchor"></span><span id="line-492" class="anchor"></span><p class="line867"><tt>OnData()</tt>的返回值应该是要导致操作——你应该返回参数<tt>default</tt>的值，除非这儿有一个错误并且你需要返回<tt>wx.DragNone</tt>。一旦你有了数据，你就可以对它作你想做的。记住，由于<tt>OnData()</tt>返回的是关于所导致操作的相关信息，而非数据本身，所以如果你想在别处使用该数据的话，你需要将数据放置在一个实例变量里面（该变量在该方法外仍然可以被访问）。 <span id="line-493" class="anchor"></span><span id="line-494" class="anchor"></span></p>
<p class="line862">在释放操作完成或取消后，返回自<tt>OnData()</tt>的导致操作类型的数据被从<tt>DoDragDrop()</tt>的返回，并且释放源的线程将继续进行。 <span id="line-495" class="anchor"></span><span id="line-496" class="anchor"></span></p>
<p class="line862">在<tt>wx.DropTarget</tt>类中有五个<tt>On...</tt>方法，你可以在你的子类中覆盖它们以在目标被调用时提供自定义的行为。我们已经见过了其中的<tt>OnData()</tt>，另外的如下： <span id="line-497" class="anchor"></span><span id="line-498" class="anchor"></span><span id="line-499" class="anchor"></span></p>
<p class="line867"><tt>OnDrop(x, y)</tt> <span id="line-500" class="anchor"></span><tt>OnEnter(x, y, default) </tt> <span id="line-501" class="anchor"></span><tt>OnDragOver(x, y,
default)</tt> <span id="line-502" class="anchor"></span><tt>OnLeave()</tt> <span id="line-503" class="anchor"></span><span id="line-504" class="anchor"></span></p>
<p class="line862">其中的参数<tt>x, y, default</tt>同<tt>OnData()</tt>。你不必覆盖这些方法，但是如果你想在你的应用程序中提供自定义的功能的话，你可以覆盖这些方法。 <span id="line-505" class="anchor"></span><span id="line-506" class="anchor"></span></p>
<p class="line862">当鼠标进入释放到的目标区域时，<tt>OnEnter()</tt>方法首先被调用。你可以使用该方法来更新一个状态窗口。该方法返回如果释放发生时要执行的操作（通常是<tt>default</tt>的值）或<tt>wx.DragNone</tt>（如果你不接受释放的话）。该方法的返回值被<tt>wxPython</tt>用来指定当鼠标移动到窗口上时，哪个图标或光标被用作显示。当鼠标位于窗口中时，方法<tt>OnDragOver()</tt>接着被调用，它返回所期望的操作或<tt>wx.DragNone</tt>。当鼠标被释放并且释放(<tt>drop)</tt>发生时，<tt>OnDrop()</tt>方法被调用，并且它默认调用<tt>OnData()</tt>。最后，当光标退出窗口时<tt>OnLeave()</tt>被调用。
<span id="line-507" class="anchor"></span><span id="line-508" class="anchor"></span></p>
<p class="line862">与数据对象一同，<tt>wxPython</tt>提供了两个预定义的释放到的目标类来涵盖最常见的情况。除了在这些情况中预定义的类会为你处理<tt>wx.DataObject</tt>，你仍然需要创建一个子类并覆盖一个方法来处理相关的数据。关于文本，类<tt>wx.TextDropTarget</tt>提供了可覆盖的方法<tt>OnDropText(x, y,
data)</tt>，你将使用通过覆盖该方法来替代覆盖<tt>OnData()</tt>。参数<tt>x,y</tt>是释放到的坐标，参数<tt>data</tt>是被释放的字符串，该字符串你可以立即使用面不用必须对数据对象作更多的查询。如果你接受新的文本的话，你的覆盖应该返回<tt>True</tt>，否则应返回<tt>False</tt>。对于文件的释放，相关的预定义的类是<tt>wx.FileDropTarget</tt>，并且可覆盖的方法是<tt>OnDropFiles(x, y,
filenames)</tt>，参数<tt>filenames</tt>是被释放的文件的名字的一个列表。另外，必要的时候你可以处理它们，当完成时可以返回<tt>True</tt>或<tt>False</tt>。 <span id="line-509" class="anchor"></span><span id="line-510" class="anchor"></span><span id="line-511" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-c91915f30b4444edb82e0f88a5223547c6eaf331">3.2. 实战释放</h3>
<span id="line-512" class="anchor"></span><span id="line-513" class="anchor"></span><span id="line-514" class="anchor"></span><p class="line874">例18.3中的代码显示了如何创建一个框架（窗口）用以接受文件的释放。你可以通过从资源管理器或查找窗口拖动一个文件到该框架（窗口）上来测试例子代码，并观查显示在窗口中的关于文件的信息。图18.3是运行后的结果。 <span id="line-515" class="anchor"></span><span id="line-516" class="anchor"></span><span id="line-517" class="anchor"></span></p>
<p class="line867"><strong>图18.3</strong> <span id="line-518" class="anchor"></span><span id="line-519" class="anchor"></span></p>
<p class="line867"><img title="attachment:w18.3.gif" src="/moin/WxPythonInAction/ChapterEighteen?action=AttachFile&amp;do=get&amp;target=w18.3.gif" class="attachment"><span id="line-520" class="anchor"></span><span id="line-521" class="anchor"></span><span id="line-522" class="anchor"></span><span id="line-523" class="anchor"></span></p>
<p class="line867"><strong>例18.3</strong>  <span id="line-524" class="anchor"></span><strong>文件释放到的目标的相关代码</strong> <span id="line-525" class="anchor"></span><span id="line-526" class="anchor"></span></p>
<p class="line867"><span id="line-527" class="anchor"></span><span id="line-528" class="anchor"></span><span id="line-529" class="anchor"></span><span id="line-530" class="anchor"></span><span id="line-531" class="anchor"></span><span id="line-532" class="anchor"></span><span id="line-533" class="anchor"></span><span id="line-534" class="anchor"></span><span id="line-535" class="anchor"></span><span id="line-536" class="anchor"></span><span id="line-537" class="anchor"></span><span id="line-538" class="anchor"></span><span id="line-539" class="anchor"></span><span id="line-540" class="anchor"></span><span id="line-541" class="anchor"></span><span id="line-542" class="anchor"></span><span id="line-543" class="anchor"></span><span id="line-544" class="anchor"></span><span id="line-545" class="anchor"></span><span id="line-546" class="anchor"></span><span id="line-547" class="anchor"></span><span id="line-548" class="anchor"></span><span id="line-549" class="anchor"></span><span id="line-550" class="anchor"></span><span id="line-551" class="anchor"></span><span id="line-552" class="anchor"></span><span id="line-553" class="anchor"></span><span id="line-554" class="anchor"></span><span id="line-555" class="anchor"></span><span id="line-556" class="anchor"></span><span id="line-557" class="anchor"></span><span id="line-558" class="anchor"></span><span id="line-559" class="anchor"></span><span id="line-560" class="anchor"></span><span id="line-561" class="anchor"></span><span id="line-562" class="anchor"></span><span id="line-563" class="anchor"></span><span id="line-564" class="anchor"></span><span id="line-565" class="anchor"></span><span id="line-566" class="anchor"></span><span id="line-567" class="anchor"></span><span id="line-568" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-4b50332975973bf82beee3d11cda3afc6686e8e9_010\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-4b50332975973bf82beee3d11cda3afc6686e8e9_010', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-4b50332975973bf82beee3d11cda3afc6686e8e9_010" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="Comment">#-*- encoding:UTF-8 -*-</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="Comment"></span><span class="ResWord">import</span> <span class="ID">wx</span></span>
<span class="line"><span class="LineNumber">   3 </span></span>
<span class="line"><span class="LineNumber">   4 </span><span class="ResWord">class</span> <span class="ID">MyFileDropTarget</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">FileDropTarget</span><span class="Operator">)</span><span class="Operator">:</span><span class="Comment">#声明释放到的目标</span></span>
<span class="line"><span class="LineNumber">   5 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">window</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   6 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">FileDropTarget</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   7 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">window</span> <span class="Operator">=</span> <span class="ID">window</span></span>
<span class="line"><span class="LineNumber">   8 </span></span>
<span class="line"><span class="LineNumber">   9 </span>    <span class="ResWord">def</span> <span class="ID">OnDropFiles</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">x</span><span class="Operator">,</span> <span class="ID">y</span><span class="Operator">,</span> <span class="ID">filenames</span><span class="Operator">)</span><span class="Operator">:</span><span class="Comment">#释放文件处理函数数据</span></span>
<span class="line"><span class="LineNumber">  10 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">window</span><span class="Operator">.</span><span class="ID">AppendText</span><span class="Operator">(</span><span class="String">"\n%d file(s) dropped at (%d,%d):\n"</span> <span class="Operator">%</span></span>
<span class="line"><span class="LineNumber">  11 </span>                               <span class="Operator">(</span><span class="ID">len</span><span class="Operator">(</span><span class="ID">filenames</span><span class="Operator">)</span><span class="Operator">,</span> <span class="ID">x</span><span class="Operator">,</span> <span class="ID">y</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  12 </span>        <span class="ResWord">for</span> <span class="ID">file</span> <span class="ResWord">in</span> <span class="ID">filenames</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  13 </span>            <span class="ID">self</span><span class="Operator">.</span><span class="ID">window</span><span class="Operator">.</span><span class="ID">AppendText</span><span class="Operator">(</span><span class="String">"\t%s\n"</span> <span class="Operator">%</span> <span class="ID">file</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  14 </span></span>
<span class="line"><span class="LineNumber">  15 </span></span>
<span class="line"><span class="LineNumber">  16 </span><span class="ResWord">class</span> <span class="ID">MyFrame</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  17 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  18 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">None</span><span class="Operator">,</span> <span class="ID">title</span><span class="Operator">=</span><span class="String">"Drop Target"</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  19 </span>                          <span class="ID">size</span><span class="Operator">=</span><span class="Operator">(</span><span class="Number">500</span><span class="Operator">,</span><span class="Number">300</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  20 </span>        <span class="ID">p</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Panel</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  21 </span></span>
<span class="line"><span class="LineNumber">  22 </span>        <span class="Comment"># create the controls</span></span>
<span class="line"><span class="LineNumber">  23 </span><span class="Comment"></span>        <span class="ID">label</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">StaticText</span><span class="Operator">(</span><span class="ID">p</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"Drop
some files here:"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  24 </span>        <span class="ID">text</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TextCtrl</span><span class="Operator">(</span><span class="ID">p</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">""</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  25 </span>                           <span class="ID">style</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">TE_MULTILINE</span><span class="Operator">|</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">HSCROLL</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  26 </span></span>
<span class="line"><span class="LineNumber">  27 </span>        <span class="Comment"># setup the layout with sizers</span></span>
<span class="line"><span class="LineNumber">  28 </span><span class="Comment"></span>        <span class="ID">sizer</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BoxSizer</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">VERTICAL</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  29 </span>        <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">label</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ALL</span><span class="Operator">,</span> <span class="Number">5</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  30 </span>        <span class="ID">sizer</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">text</span><span class="Operator">,</span> <span class="Number">1</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">EXPAND</span><span class="Operator">|</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">ALL</span><span class="Operator">,</span> <span class="Number">5</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  31 </span>        <span class="ID">p</span><span class="Operator">.</span><span class="ID">SetSizer</span><span class="Operator">(</span><span class="ID">sizer</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  32 </span></span>
<span class="line"><span class="LineNumber">  33 </span>        <span class="Comment"># make the text control be a drop target</span></span>
<span class="line"><span class="LineNumber">  34 </span><span class="Comment"></span>        <span class="ID">dt</span> <span class="Operator">=</span> <span class="ID">MyFileDropTarget</span><span class="Operator">(</span><span class="ID">text</span><span class="Operator">)</span><span class="Comment">#将文本控件作为释放到的目标</span></span>
<span class="line"><span class="LineNumber">  35 </span>        <span class="ID">text</span><span class="Operator">.</span><span class="ID">SetDropTarget</span><span class="Operator">(</span><span class="ID">dt</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  36 </span></span>
<span class="line"><span class="LineNumber">  37 </span></span>
<span class="line"><span class="LineNumber">  38 </span><span class="ID">app</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">PySimpleApp</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  39 </span><span class="ID">frm</span> <span class="Operator">=</span> <span class="ID">MyFrame</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  40 </span><span class="ID">frm</span><span class="Operator">.</span><span class="ID">Show</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  41 </span><span class="ID">app</span><span class="Operator">.</span><span class="ID">MainLoop</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-569" class="anchor"></span><span id="line-570" class="anchor"></span><span id="line-571" class="anchor"></span><p class="line862">到目前为止，我们还是局限于对<tt>wxPython</tt>的预定义的对象的数据传送的讨论。接下来，我们将讨论如何将你自己的数据放到剪贴板上。 <span id="line-572" class="anchor"></span><span id="line-573" class="anchor"></span><span id="line-574" class="anchor"></span><span id="line-575" class="anchor"></span></p>
<p class="line867">
</p>
<h2 id="head-99b850b19106a60abc32c36ced046f647ba62803">4. 传送自定义对象</h2>
<span id="line-576" class="anchor"></span><span id="line-577" class="anchor"></span><span id="line-578" class="anchor"></span><p class="line862">使用<tt>wxPython</tt>的预定义的数据对象，你只能工作于纯文本、位图或文件。而更有创建性的是，你应该让你自定义的对象能够在应用之间被传送。在这一节，我将给你展示如何给你的<tt>wxPython</tt>应用程序增加更高级的性能，如传送自定义的数据对象和以多种格式传送一个对象。 <span id="line-579" class="anchor"></span><span id="line-580" class="anchor"></span><span id="line-581" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-ae4e26407da3311c801990377a9c8566965882d0">4.1. 传送自定义的数据对象</h3>
<span id="line-582" class="anchor"></span><span id="line-583" class="anchor"></span><span id="line-584" class="anchor"></span><p class="line862">尽管文本、位图的数据对象和文件名的列表对于不同的使用已经足够了，但有时你仍然需要传送自定义的对象，如你自己的图形格式或一个自定义的数据结构。接下来，在保留对你的对象将接受的数据的类型的控制时，我们将涉及传送自定义数据对象的机制。该方法的局限是它只能工作在<tt>wxPython</tt>内，你不能使用这个方法来让其它的应用程序去读你的自定义的格式。要将<tt>RTF</tt>（丰富文本格式）传送给<tt>Microsoft Word</tt>，该机制将不工作。 <span id="line-585" class="anchor"></span><span id="line-586" class="anchor"></span></p>
<p class="line862">要实现自定义的数据传送，我们将使用<tt>wxPython</tt>的类<tt>wx.CustomDataObject</tt>，它被设计来用于处理任意的数据。<tt>wx.CustomDataObject</tt>的构造器如下： <span id="line-587" class="anchor"></span><span id="line-588" class="anchor"></span></p>
<p class="line867"><span id="line-589" class="anchor"></span><span id="line-590" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-0f8c0fa5b77755f42c59a30fefebd993d671fd52_011\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-0f8c0fa5b77755f42c59a30fefebd993d671fd52_011', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-0f8c0fa5b77755f42c59a30fefebd993d671fd52_011" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">CustomDataObject</span><span class="Operator">(</span><span class="ID">format</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">FormatInvalid</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-591" class="anchor"></span><span id="line-592" class="anchor"></span><p class="line862">参数<tt>format</tt>技术上应该是类<tt>wx.DataFormat</tt>的一个实例，但为了我们的目的，我们可以只给它传递一个字符串，数据类型的责任由<tt>wxPython</tt>来考虑。我们只需要这个字符串作为自定义格式的一个标签，以与其它的区分开来。一旦我们有了我们自定义的数据实例，我们就可以使用方法<tt>SetData(data)</tt>将数据放入到自定义的数据实例中。参数<tt>data</tt>必须是一个字符串。下面是一段样板代码： <span id="line-593" class="anchor"></span><span id="line-594" class="anchor"></span></p>
<p class="line867"><span id="line-595" class="anchor"></span><span id="line-596" class="anchor"></span><span id="line-597" class="anchor"></span><span id="line-598" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-33e904f276db8662e185431b73497330dfb16587_012\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-33e904f276db8662e185431b73497330dfb16587_012', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-33e904f276db8662e185431b73497330dfb16587_012" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">data_object</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">CustomDataObject</span><span class="Operator">(</span><span class="String">"MyNiftyFormat"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ID">data</span> <span class="Operator">=</span> <span class="ID">cPickle</span><span class="Operator">.</span><span class="ID">dumps</span><span class="Operator">(</span><span class="ID">my_object</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="ID">data_object</span><span class="Operator">.</span><span class="ID">SetData</span><span class="Operator">(</span><span class="ID">data</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-599" class="anchor"></span><span id="line-600" class="anchor"></span><p class="line862">在这段代码片断之后，你可以将<tt>data_object</tt>传递到剪贴板或另一个数据源，以继续数据的传送。 <span id="line-601" class="anchor"></span><span id="line-602" class="anchor"></span><span id="line-603" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-f748870f5c53aa1912f8504867939c750547811f">4.2. 得到自定义对象</h3>
<span id="line-604" class="anchor"></span><span id="line-605" class="anchor"></span><span id="line-606" class="anchor"></span><p class="line862">要得到该对象，需要执行相同的基本步骤。对于从剪贴板获取，先创建相同格式的一个自定义数据对象，然后得到数据并对得到的数据进行逆<tt>pickle</tt>操作（<tt>pickle</tt>有加工的意思）。 <span id="line-607" class="anchor"></span><span id="line-608" class="anchor"></span></p>
<p class="line867"><span id="line-609" class="anchor"></span><span id="line-610" class="anchor"></span><span id="line-611" class="anchor"></span><span id="line-612" class="anchor"></span><span id="line-613" class="anchor"></span><span id="line-614" class="anchor"></span><span id="line-615" class="anchor"></span><span id="line-616" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-bb8b118fb1ef4f107af808622b3d375783d463bf_013\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-bb8b118fb1ef4f107af808622b3d375783d463bf_013', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-bb8b118fb1ef4f107af808622b3d375783d463bf_013" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">data_object</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">CustomDataObject</span><span class="Operator">(</span><span class="String">"MyNiftyFormat"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ResWord">if</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">Open</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   3 </span>    <span class="ID">success</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">GetData</span><span class="Operator">(</span><span class="ID">data_object</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   4 </span>    <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TheClipboard</span><span class="Operator">.</span><span class="ID">Close</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   5 </span><span class="ResWord">if</span> <span class="ID">success</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   6 </span>    <span class="ID">pickled_data</span> <span class="Operator">=</span> <span class="ID">data_object</span><span class="Operator">.</span><span class="ID">GetData</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   7 </span>    <span class="ID">object</span> <span class="Operator">=</span> <span class="ID">cPickle</span><span class="Operator">.</span><span class="ID">loads</span><span class="Operator">(</span><span class="ID">pickled_data</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-617" class="anchor"></span><span id="line-618" class="anchor"></span><p class="line862">拖放工作是类似的。使用已<tt>pickle</tt>的数据设置释放源的数据对象，并将设置的数据对象给你的自定义的数据对象，数据的目标在它的<tt>OnData()</tt>方法中对数据进行逆<tt>pickle</tt>操作并把数据放到有用的地方。 <span id="line-619" class="anchor"></span><span id="line-620" class="anchor"></span></p>
<p class="line862">创建自定义对象的另一个方法是建造你自己的<tt>wx.DataObject</tt>子类。如果你选择这条途径，那么你会希望实现你自己的诸如<tt>wx.PyDataObjectSimple</tt>（用于通常的对象），或<tt>wx.PyTextDataObject</tt>，<tt>wx.PyBitmapDataObject, </tt>或<tt>wx.PyFileDataObject</tt>的一个子类。这将使你能够覆盖所有必要的方法。 <span id="line-621" class="anchor"></span><span id="line-622" class="anchor"></span><span id="line-623" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-4f0ccd0ace0ce997058c1697da9aa11053d44f00">4.3. 以多种格式传送对象</h3>
<span id="line-624" class="anchor"></span><span id="line-625" class="anchor"></span><span id="line-626" class="anchor"></span><p class="line862">使用<tt>wxPython</tt>的数据对象来用于数据传送的最大好处是，数据对象了解数据格式。一个数据对象甚至能够用多种的格式来管理相同的数据。例如，你可能希望你自己的应用程序能够接受你的自定义的文本格式对象的数据，但是你仍然希望其它的应用能够以纯文本的格式接受该数据。 <span id="line-627" class="anchor"></span><span id="line-628" class="anchor"></span></p>
<p class="line862">管理该功能的机制是类<tt>wx.DataObjectComposite</tt>。目前，我们所见过的所有被继承的数据对象都是<tt>wx.DataObjectSimple</tt>的子类。<tt>wx.DataObjectComposite</tt>的目的是将任意数量的简单数据对象合并为一个数据对象。该合并后的对象能够将它的数据提供给与构成它的任一简单类型匹配的一个数据对象。 <span id="line-629" class="anchor"></span><span id="line-630" class="anchor"></span></p>
<p class="line862">要建造一个合成的数据对象，首先要使用一个无参的构造器<tt>wx.DataObjectComposite()</tt>作为开始，然后使用<tt>Add(data, preferred=False)</tt>分别增加简单数据对象。要建造一个合并了你的自定义格式和纯文本的数据对象，可以如下这样： <span id="line-631" class="anchor"></span><span id="line-632" class="anchor"></span></p>
<p class="line867"><span id="line-633" class="anchor"></span><span id="line-634" class="anchor"></span><span id="line-635" class="anchor"></span><span id="line-636" class="anchor"></span><span id="line-637" class="anchor"></span><span id="line-638" class="anchor"></span><span id="line-639" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-fac93ab19f6645e312516ab917bd366ae6ddd876_014\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-fac93ab19f6645e312516ab917bd366ae6ddd876_014', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-fac93ab19f6645e312516ab917bd366ae6ddd876_014" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">data_object</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">CustomDataObject</span><span class="Operator">(</span><span class="String">"MyNiftyFormat"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ID">data_object</span><span class="Operator">.</span><span class="ID">SetData</span><span class="Operator">(</span><span class="ID">cPickle</span><span class="Operator">.</span><span class="ID">dumps</span><span class="Operator">(</span><span class="ID">my_object</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="ID">text_object</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TextDataObject</span><span class="Operator">(</span><span class="ID">str</span><span class="Operator">(</span><span class="ID">my_object</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   4 </span><span class="ID">composite</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">DataObjectComposite</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   5 </span><span class="ID">composite</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">data_object</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   6 </span><span class="ID">composite</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">text_object</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-640" class="anchor"></span><span id="line-641" class="anchor"></span><p class="line862">此后，将这个合成的对象传递给剪贴板或你的释放源。如果目标类要求一个使用了自定义格式的对象，那么它接受已<tt>pickle</tt>的对象。如果它要求纯文本的数据，那么它得到字符串表达式。 <span id="line-642" class="anchor"></span><span id="line-643" class="anchor"></span></p>
<p class="line874">下节内容：我们将给你展示如何使用一个定时器来管理定时事件。 <span id="line-644" class="anchor"></span><span id="line-645" class="anchor"></span><span id="line-646" class="anchor"></span></p>
<p class="line867">
</p>
<h2 id="head-879e24ef5b84f550e3e8d4414f56a8079b374db2">5. 使用wx.Timer来设置定时事件</h2>
<span id="line-647" class="anchor"></span><span id="line-648" class="anchor"></span><span id="line-649" class="anchor"></span><p class="line862">有时你需要让你的应用程序产生基于时间段的事件。要得到这个功能，你可以使用类<tt>wx.Timer</tt>。 <span id="line-650" class="anchor"></span><span id="line-651" class="anchor"></span><span id="line-652" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-edbab7459adf6b2c54a26ca188dfd068c29094ee">5.1. 产生EVT_TIMER事件</h3>
<span id="line-653" class="anchor"></span><span id="line-654" class="anchor"></span><span id="line-655" class="anchor"></span><p class="line862">对<tt>wx.Timer</tt>最灵活和最有效的用法是使它产生<tt>EVT_TIMER</tt>，并将该事件如同其它事件一样进行绑定。 <span id="line-656" class="anchor"></span><span id="line-657" class="anchor"></span><span id="line-658" class="anchor"></span></p>
<p class="line867"><strong>创建定时器</strong> <span id="line-659" class="anchor"></span><span id="line-660" class="anchor"></span></p>
<p class="line862">要创建一个定时器，首先要使用下面的构造器来创建一个<tt>wx.Timer</tt>的实例。 <span id="line-661" class="anchor"></span><span id="line-662" class="anchor"></span></p>
<p class="line867"><span id="line-663" class="anchor"></span><span id="line-664" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-c2984e9e93730ebc3b84ae9bfb7a94d257568c41_015\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-c2984e9e93730ebc3b84ae9bfb7a94d257568c41_015', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-c2984e9e93730ebc3b84ae9bfb7a94d257568c41_015" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Timer</span><span class="Operator">(</span><span class="ID">owner</span><span class="Operator">=</span><span class="ID">None</span><span class="Operator">,</span> <span class="ID">id</span><span class="Operator">=</span><span class="Operator">-</span><span class="Number">1</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-665" class="anchor"></span><span id="line-666" class="anchor"></span><p class="line862">其中参数<tt>owner</tt>是实现<tt>wx.EvtHandler</tt>的实例，即任一能够接受事件通知的<tt>wxPython</tt>控件或其它的东西。参数<tt>id</tt>用于区分不同的定时器。如果没有指定<tt>id</tt>，则<tt>wxPython</tt>会为你生成一个<tt>id</tt>号。如果当你创建定时器时，你不想设置参数<tt>owner</tt>和<tt>id</tt>，那么你可以以后随时使用<tt>SetOwner(owner=None,
id=</tt>-1)方法来设置，它设置同样的两个参数。 <span id="line-667" class="anchor"></span><span id="line-668" class="anchor"></span><span id="line-669" class="anchor"></span></p>
<p class="line867"><strong>绑定定时器</strong> <span id="line-670" class="anchor"></span><span id="line-671" class="anchor"></span></p>
<p class="line862">在你创建了定时器之后，你可以如下面一行的代码来在你的事件处理控件中绑定<tt>wx.EVT_TIMER</tt>事件。 <span id="line-672" class="anchor"></span><span id="line-673" class="anchor"></span></p>
<p class="line867"><span id="line-674" class="anchor"></span><span id="line-675" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-4e0ea3a5bcb4974466c02afbe899ff6acdb47b44_016\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-4e0ea3a5bcb4974466c02afbe899ff6acdb47b44_016', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-4e0ea3a5bcb4974466c02afbe899ff6acdb47b44_016" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_TIMER</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnTimerEvent</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-676" class="anchor"></span><span id="line-677" class="anchor"></span><p class="line862">如果你需要绑定多个定时器到多个处理函数，你可以给<tt>Bind</tt>函数传递每个定时器的<tt>ID</tt>，或将定时器对象作为源参数来传递。 <span id="line-678" class="anchor"></span><span id="line-679" class="anchor"></span></p>
<p class="line867"><span id="line-680" class="anchor"></span><span id="line-681" class="anchor"></span><span id="line-682" class="anchor"></span><span id="line-683" class="anchor"></span><span id="line-684" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-1cc89bf509cf0019bee05e8e5ffabaf21f599118_017\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-1cc89bf509cf0019bee05e8e5ffabaf21f599118_017', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-1cc89bf509cf0019bee05e8e5ffabaf21f599118_017" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">timer1</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Timer</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="ID">timer2</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Timer</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_TIMER</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnTimer1Event</span><span class="Operator">,</span> <span class="ID">timer1</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   4 </span><span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_TIMER</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnTimer2Event</span><span class="Operator">,</span> <span class="ID">timer2</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-685" class="anchor"></span><span id="line-686" class="anchor"></span><span id="line-687" class="anchor"></span><p class="line867"><strong>启动和停止定时器</strong> <span id="line-688" class="anchor"></span><span id="line-689" class="anchor"></span></p>
<p class="line862">在定时器事件被绑定后，你所需要做的所有事情就是启动该定时器，使用方法<tt>Start(milliseconds=</tt>-1,
<tt>oneShot=False)</tt>。其中参数<tt>milliseconds</tt>是毫秒数。这将在经过<tt>milliseconds</tt>时间后，产生一个<tt>wx.EVT_TIMER</tt>事件。如果<tt>milliseconds=</tt>-1，那么将使用早先的毫秒数。如果<tt>oneShot</tt>为<tt>True</tt>，那么定时器只产生<tt>wx.EVT_TIMER</tt>事件一次，然后定时器停止。否则，你必须显式地使用<tt>Stop()</tt>方法来停止定时器。
<span id="line-690" class="anchor"></span>例18.4使用了定时器机制来驱动一个数字时钟，并每秒刷新一次显示。 <span id="line-691" class="anchor"></span><span id="line-692" class="anchor"></span><span id="line-693" class="anchor"></span></p>
<p class="line867"><strong>例18.4</strong>  <span id="line-694" class="anchor"></span><strong>一个简单的数字时钟</strong> <span id="line-695" class="anchor"></span><span id="line-696" class="anchor"></span></p>
<p class="line867"><span id="line-697" class="anchor"></span><span id="line-698" class="anchor"></span><span id="line-699" class="anchor"></span><span id="line-700" class="anchor"></span><span id="line-701" class="anchor"></span><span id="line-702" class="anchor"></span><span id="line-703" class="anchor"></span><span id="line-704" class="anchor"></span><span id="line-705" class="anchor"></span><span id="line-706" class="anchor"></span><span id="line-707" class="anchor"></span><span id="line-708" class="anchor"></span><span id="line-709" class="anchor"></span><span id="line-710" class="anchor"></span><span id="line-711" class="anchor"></span><span id="line-712" class="anchor"></span><span id="line-713" class="anchor"></span><span id="line-714" class="anchor"></span><span id="line-715" class="anchor"></span><span id="line-716" class="anchor"></span><span id="line-717" class="anchor"></span><span id="line-718" class="anchor"></span><span id="line-719" class="anchor"></span><span id="line-720" class="anchor"></span><span id="line-721" class="anchor"></span><span id="line-722" class="anchor"></span><span id="line-723" class="anchor"></span><span id="line-724" class="anchor"></span><span id="line-725" class="anchor"></span><span id="line-726" class="anchor"></span><span id="line-727" class="anchor"></span><span id="line-728" class="anchor"></span><span id="line-729" class="anchor"></span><span id="line-730" class="anchor"></span><span id="line-731" class="anchor"></span><span id="line-732" class="anchor"></span><span id="line-733" class="anchor"></span><span id="line-734" class="anchor"></span><span id="line-735" class="anchor"></span><span id="line-736" class="anchor"></span><span id="line-737" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-43a5348a2cffccdb5b0a1b787d6111c943460397_018\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-43a5348a2cffccdb5b0a1b787d6111c943460397_018', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-43a5348a2cffccdb5b0a1b787d6111c943460397_018" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="Comment">#-*- encoding:UTF-8 -*-</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="Comment"></span><span class="ResWord">import</span> <span class="ID">wx</span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="ResWord">import</span> <span class="ID">time</span></span>
<span class="line"><span class="LineNumber">   4 </span></span>
<span class="line"><span class="LineNumber">   5 </span><span class="ResWord">class</span> <span class="ID">ClockWindow</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Window</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   6 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">parent</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   7 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Window</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">parent</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   8 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_PAINT</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnPaint</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">   9 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">timer</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Timer</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Comment">#创建定时器</span></span>
<span class="line"><span class="LineNumber">  10 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_TIMER</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnTimer</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">timer</span><span class="Operator">)</span><span class="Comment">#绑定一个定时器事件</span></span>
<span class="line"><span class="LineNumber">  11 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">timer</span><span class="Operator">.</span><span class="ID">Start</span><span class="Operator">(</span><span class="Number">1000</span><span class="Operator">)</span><span class="Comment">#设定时间间隔</span></span>
<span class="line"><span class="LineNumber">  12 </span></span>
<span class="line"><span class="LineNumber">  13 </span>    <span class="ResWord">def</span> <span class="ID">Draw</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">dc</span><span class="Operator">)</span><span class="Operator">:</span><span class="Comment">#绘制当前时间</span></span>
<span class="line"><span class="LineNumber">  14 </span>        <span class="ID">t</span> <span class="Operator">=</span> <span class="ID">time</span><span class="Operator">.</span><span class="ID">localtime</span><span class="Operator">(</span><span class="ID">time</span><span class="Operator">.</span><span class="ID">time</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  15 </span>        <span class="ID">st</span> <span class="Operator">=</span> <span class="ID">time</span><span class="Operator">.</span><span class="ID">strftime</span><span class="Operator">(</span><span class="String">"%I:%M:%S"</span><span class="Operator">,</span> <span class="ID">t</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  16 </span>        <span class="ID">w</span><span class="Operator">,</span> <span class="ID">h</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">GetClientSize</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  17 </span>        <span class="ID">dc</span><span class="Operator">.</span><span class="ID">SetBackground</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Brush</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">GetBackgroundColour</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  18 </span>        <span class="ID">dc</span><span class="Operator">.</span><span class="ID">Clear</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  19 </span>        <span class="ID">dc</span><span class="Operator">.</span><span class="ID">SetFont</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Font</span><span class="Operator">(</span><span class="Number">30</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">SWISS</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">NORMAL</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">NORMAL</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  20 </span>        <span class="ID">tw</span><span class="Operator">,</span> <span class="ID">th</span> <span class="Operator">=</span> <span class="ID">dc</span><span class="Operator">.</span><span class="ID">GetTextExtent</span><span class="Operator">(</span><span class="ID">st</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  21 </span>        <span class="ID">dc</span><span class="Operator">.</span><span class="ID">DrawText</span><span class="Operator">(</span><span class="ID">st</span><span class="Operator">,</span> <span class="Operator">(</span><span class="ID">w</span><span class="Operator">-</span><span class="ID">tw</span><span class="Operator">)</span><span class="Operator">/</span><span class="Number">2</span><span class="Operator">,</span> <span class="Operator">(</span><span class="ID">h</span><span class="Operator">)</span><span class="Operator">/</span><span class="Number">2</span> <span class="Operator">-</span> <span class="ID">th</span><span class="Operator">/</span><span class="Number">2</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  22 </span></span>
<span class="line"><span class="LineNumber">  23 </span>    <span class="ResWord">def</span> <span class="ID">OnTimer</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span><span class="Comment">#显示时间事件处理函数</span></span>
<span class="line"><span class="LineNumber">  24 </span>        <span class="ID">dc</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BufferedDC</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">ClientDC</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  25 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Draw</span><span class="Operator">(</span><span class="ID">dc</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  26 </span></span>
<span class="line"><span class="LineNumber">  27 </span>    <span class="ResWord">def</span> <span class="ID">OnPaint</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  28 </span>        <span class="ID">dc</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BufferedPaintDC</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  29 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Draw</span><span class="Operator">(</span><span class="ID">dc</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  30 </span></span>
<span class="line"><span class="LineNumber">  31 </span><span class="ResWord">class</span> <span class="ID">MyFrame</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  32 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  33 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">None</span><span class="Operator">,</span> <span class="ID">title</span><span class="Operator">=</span><span class="String">"wx.Timer"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  34 </span>        <span class="ID">ClockWindow</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  35 </span></span>
<span class="line"><span class="LineNumber">  36 </span></span>
<span class="line"><span class="LineNumber">  37 </span><span class="ID">app</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">PySimpleApp</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  38 </span><span class="ID">frm</span> <span class="Operator">=</span> <span class="ID">MyFrame</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  39 </span><span class="ID">frm</span><span class="Operator">.</span><span class="ID">Show</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  40 </span><span class="ID">app</span><span class="Operator">.</span><span class="ID">MainLoop</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-738" class="anchor"></span><span id="line-739" class="anchor"></span><span id="line-740" class="anchor"></span><p class="line867"><strong>确定当前定时器的状态</strong> <span id="line-741" class="anchor"></span><span id="line-742" class="anchor"></span></p>
<p class="line862">你可以使用方法<tt>IsRunning()</tt>来确定定时器的当前状态，使用方法<tt>GetInterval()</tt>来得到当前的时间间隔。如果定时器正在运行并且只运行一次的话，方法<tt>IsOneShot()</tt>返回<tt>True</tt>。 <span id="line-743" class="anchor"></span><span id="line-744" class="anchor"></span></p>
<p class="line867"><tt>wx.TimerEvent</tt>几乎与它的父类<tt>wx.Event</tt>是一样的，除了它不包括<tt>wx.GetInterval()</tt>方法来返回定时器的时间间隔外。万一你将来自多个定时器的事件绑定给了相同的处理函数，并希望根据特定的定时器的事件来做不同的动作的话，可使用事件方法<tt>GetId()</tt>来返回定时器的<tt>ID</tt>，以区别对待。
<span id="line-745" class="anchor"></span><span id="line-746" class="anchor"></span><span id="line-747" class="anchor"></span><span id="line-748" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-11620340b8a7a1cc4fe7f3471129263ab388b157">5.2. 学习定时器的其它用法</h3>
<span id="line-749" class="anchor"></span><span id="line-750" class="anchor"></span><span id="line-751" class="anchor"></span><p class="line862">另一种使用定时器的方法是子类化<tt>wx.Timer</tt>。在你的子类中你可以覆盖方法<tt>Notify()</tt>。在父类中，该方法每次在定时器经过指定的时间间隔后被自动调用，它触发定时器事件。然而你的子类没有义务去触发一个定时器事件，你可以在该<tt>Notify()</tt>方法中做你想做的事，以响应定时器的时间间隔。 <span id="line-752" class="anchor"></span><span id="line-753" class="anchor"></span></p>
<p class="line862">要在未来某时触发一个特定的行为，有一个被称为<tt>wx.FutureCall</tt>的类可以使用。它的构造器如下： <span id="line-754" class="anchor"></span><span id="line-755" class="anchor"></span></p>
<p class="line867"><span id="line-756" class="anchor"></span><span id="line-757" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-13e532297d60d6546709e8361bc0abca3b9c1b9e_019\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-13e532297d60d6546709e8361bc0abca3b9c1b9e_019', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-13e532297d60d6546709e8361bc0abca3b9c1b9e_019" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">FutureCall</span><span class="Operator">(</span><span class="ID">interval</span><span class="Operator">,</span> <span class="ID">callable</span><span class="Operator">,</span> <span class="Operator">*</span><span class="ID">args</span><span class="Operator">,</span> <span class="Operator">**</span><span class="ID">kwargs</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-758" class="anchor"></span><span id="line-759" class="anchor"></span><p class="line862">一旦它被创建后，<tt>wx.FutureCall</tt>的实例将等待<tt>interval</tt>毫秒，然后调用传递给参数<tt>callable</tt>的对象，参数*<tt>args,
</tt>**<tt>kwargs</tt>是<tt>callable</tt>中的对象所要使用的。<tt>wx.FutureCall</tt>只触发一次定时事件。 <span id="line-760" class="anchor"></span><span id="line-761" class="anchor"></span></p>
<p class="line862">下节内容提示：创建一个多线程的<tt>wxPython</tt>应用程序 <span id="line-762" class="anchor"></span><span id="line-763" class="anchor"></span><span id="line-764" class="anchor"></span><span id="line-765" class="anchor"></span></p>
<p class="line867">
</p>
<h2 id="head-842bfd4c2283e3bcfd4ffd21c73bc78685d1be04">6. 创建一个多线程的wxPython应用程序</h2>
<span id="line-766" class="anchor"></span><span id="line-767" class="anchor"></span><span id="line-768" class="anchor"></span><p class="line862">在大多数的<tt>GUI</tt>应用程序中，在应用程序的后台中长期执行一个处理过程而不干涉用户与应用程序的其它部分的交互是有好处的。允许后台处理的机制通常是产生一个线程并在该线程中长期执行一个处理过程。对于<tt>wxPython</tt>的多线程，在这一节我们有两点需要特别说明。 <span id="line-769" class="anchor"></span><span id="line-770" class="anchor"></span></p>
<p class="line862">最重要的一点是，<tt>GUI</tt>的操作必须发生在主线程或应用程序的主循环所处的地方中。在一个单独的线程中执行<tt>GUI</tt>操作对于无法预知的程序崩溃和调试来说是一个好的办法。基于技术方面的原因，如许多<tt>Unix</tt>的<tt>GUI</tt>库不是线程安全性的，以及在微软<tt>Windows</tt>下<tt>UI</tt>对象的创建问题，<tt>wxPython</tt>没有设计它自己的发生在多线程中的事件，所以我们建议你也不要尝试。
<span id="line-771" class="anchor"></span><span id="line-772" class="anchor"></span></p>
<p class="line862">上面的禁令包括与屏幕交互的任何项目，尤其包括<tt>wx.Bitmap</tt>对象。 <span id="line-773" class="anchor"></span><span id="line-774" class="anchor"></span></p>
<p class="line862">对于<tt>wxPython</tt>应用程序，关于所有<tt>UI</tt>的更新，后台线程只负责发送消息给<tt>UI</tt>线程，而不关心<tt>GUI</tt>的更新。幸运的是，<tt>wxPython</tt>没有强制限定你能够有的后台线程的数量。 <span id="line-775" class="anchor"></span><span id="line-776" class="anchor"></span></p>
<p class="line862">在这一节，我们将关注几个<tt>wxPython</tt>中实现多线程的方法。最常用的技术是使用<tt>wx.CallAfter()</tt>函数，一会我们会讨论它。然后，我们将看一看如何使用<tt>Python</tt>的队列对象来设置一个并行事件队列。最后，我们将讨论如何为多线程开发一个定制的解决方案。 <span id="line-777" class="anchor"></span><span id="line-778" class="anchor"></span><span id="line-779" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-06221da266b8caebd560d7f458bd65594e27e1a0">6.1. 使用全局函数wx.CallAfter()</h3>
<span id="line-780" class="anchor"></span><span id="line-781" class="anchor"></span><span id="line-782" class="anchor"></span><p class="line862">例18.5显示了一个使用线程的例子，它使用了<tt>wxPython</tt>的全局函数<tt>wx.CallAfter()</tt>，该函数是传递消息给你的主线程的最容易的方法。<tt>wx.CallAfter()</tt>使得主线程在当前的事件处理完成后，可以对一个不同的线程调用一个函数。传递给<tt>wx.CallAfter()</tt>的函数对象总是在主线程中被执行。
<span id="line-783" class="anchor"></span><span id="line-784" class="anchor"></span></p>
<p class="line874">图18.4显示了多线程窗口的运行结果。 <span id="line-785" class="anchor"></span><span id="line-786" class="anchor"></span><span id="line-787" class="anchor"></span></p>
<p class="line867"><strong>图18.4</strong> <span id="line-788" class="anchor"></span><span id="line-789" class="anchor"></span></p>
<p class="line867"><img title="attachment:w18.4.gif" src="/moin/WxPythonInAction/ChapterEighteen?action=AttachFile&amp;do=get&amp;target=w18.4.gif" class="attachment"><span id="line-790" class="anchor"></span><span id="line-791" class="anchor"></span><span id="line-792" class="anchor"></span></p>
<p class="line874">例18.5显示了产生图18.4的代码 <span id="line-793" class="anchor"></span><span id="line-794" class="anchor"></span><span id="line-795" class="anchor"></span></p>
<p class="line867"><strong>例18.5</strong>  <span id="line-796" class="anchor"></span><strong>使用<tt>wx.CallAfter()</tt>来传递消息给主线程的一个线程例子</strong> <span id="line-797" class="anchor"></span><span id="line-798" class="anchor"></span></p>
<p class="line867"><span id="line-799" class="anchor"></span><span id="line-800" class="anchor"></span><span id="line-801" class="anchor"></span><span id="line-802" class="anchor"></span><span id="line-803" class="anchor"></span><span id="line-804" class="anchor"></span><span id="line-805" class="anchor"></span><span id="line-806" class="anchor"></span><span id="line-807" class="anchor"></span><span id="line-808" class="anchor"></span><span id="line-809" class="anchor"></span><span id="line-810" class="anchor"></span><span id="line-811" class="anchor"></span><span id="line-812" class="anchor"></span><span id="line-813" class="anchor"></span><span id="line-814" class="anchor"></span><span id="line-815" class="anchor"></span><span id="line-816" class="anchor"></span><span id="line-817" class="anchor"></span><span id="line-818" class="anchor"></span><span id="line-819" class="anchor"></span><span id="line-820" class="anchor"></span><span id="line-821" class="anchor"></span><span id="line-822" class="anchor"></span><span id="line-823" class="anchor"></span><span id="line-824" class="anchor"></span><span id="line-825" class="anchor"></span><span id="line-826" class="anchor"></span><span id="line-827" class="anchor"></span><span id="line-828" class="anchor"></span><span id="line-829" class="anchor"></span><span id="line-830" class="anchor"></span><span id="line-831" class="anchor"></span><span id="line-832" class="anchor"></span><span id="line-833" class="anchor"></span><span id="line-834" class="anchor"></span><span id="line-835" class="anchor"></span><span id="line-836" class="anchor"></span><span id="line-837" class="anchor"></span><span id="line-838" class="anchor"></span><span id="line-839" class="anchor"></span><span id="line-840" class="anchor"></span><span id="line-841" class="anchor"></span><span id="line-842" class="anchor"></span><span id="line-843" class="anchor"></span><span id="line-844" class="anchor"></span><span id="line-845" class="anchor"></span><span id="line-846" class="anchor"></span><span id="line-847" class="anchor"></span><span id="line-848" class="anchor"></span><span id="line-849" class="anchor"></span><span id="line-850" class="anchor"></span><span id="line-851" class="anchor"></span><span id="line-852" class="anchor"></span><span id="line-853" class="anchor"></span><span id="line-854" class="anchor"></span><span id="line-855" class="anchor"></span><span id="line-856" class="anchor"></span><span id="line-857" class="anchor"></span><span id="line-858" class="anchor"></span><span id="line-859" class="anchor"></span><span id="line-860" class="anchor"></span><span id="line-861" class="anchor"></span><span id="line-862" class="anchor"></span><span id="line-863" class="anchor"></span><span id="line-864" class="anchor"></span><span id="line-865" class="anchor"></span><span id="line-866" class="anchor"></span><span id="line-867" class="anchor"></span><span id="line-868" class="anchor"></span><span id="line-869" class="anchor"></span><span id="line-870" class="anchor"></span><span id="line-871" class="anchor"></span><span id="line-872" class="anchor"></span><span id="line-873" class="anchor"></span><span id="line-874" class="anchor"></span><span id="line-875" class="anchor"></span><span id="line-876" class="anchor"></span><span id="line-877" class="anchor"></span><span id="line-878" class="anchor"></span><span id="line-879" class="anchor"></span><span id="line-880" class="anchor"></span><span id="line-881" class="anchor"></span><span id="line-882" class="anchor"></span><span id="line-883" class="anchor"></span><span id="line-884" class="anchor"></span><span id="line-885" class="anchor"></span><span id="line-886" class="anchor"></span><span id="line-887" class="anchor"></span><span id="line-888" class="anchor"></span><span id="line-889" class="anchor"></span><span id="line-890" class="anchor"></span><span id="line-891" class="anchor"></span><span id="line-892" class="anchor"></span><span id="line-893" class="anchor"></span><span id="line-894" class="anchor"></span><span id="line-895" class="anchor"></span><span id="line-896" class="anchor"></span><span id="line-897" class="anchor"></span><span id="line-898" class="anchor"></span><span id="line-899" class="anchor"></span><span id="line-900" class="anchor"></span><span id="line-901" class="anchor"></span></p>
<div lang="en" dir="ltr" class="codearea">
<script type="text/javascript">
document.write('<a href="#" onclick="return togglenumber(\'CA-1a25eabbc23edd2bf03d8a85483fe1c73a8d3e8f_020\', 1, 1);" \
class="codenumbers">切换行号显示<\/a>');
</script><a class="codenumbers" onclick="return togglenumber('CA-1a25eabbc23edd2bf03d8a85483fe1c73a8d3e8f_020', 1, 1);" href="#">切换行号显示</a>
<pre lang="en" id="CA-1a25eabbc23edd2bf03d8a85483fe1c73a8d3e8f_020" dir="ltr"><span class="line"><span class="LineNumber">   1 </span><span class="Comment">#-*- encoding:UTF-8 -*-</span></span>
<span class="line"><span class="LineNumber">   2 </span><span class="Comment"></span><span class="ResWord">import</span> <span class="ID">wx</span></span>
<span class="line"><span class="LineNumber">   3 </span><span class="ResWord">import</span> <span class="ID">threading</span></span>
<span class="line"><span class="LineNumber">   4 </span><span class="ResWord">import</span> <span class="ID">random</span></span>
<span class="line"><span class="LineNumber">   5 </span></span>
<span class="line"><span class="LineNumber">   6 </span><span class="ResWord">class</span> <span class="ID">WorkerThread</span><span class="Operator">(</span><span class="ID">threading</span><span class="Operator">.</span><span class="ID">Thread</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">   7 </span>    <span class="String">"""</span></span>
<span class="line"><span class="LineNumber">   8 </span><span class="String">    This just simulates some long-running task that periodically sends</span></span>
<span class="line"><span class="LineNumber">   9 </span><span class="String">    a message to the GUI thread.</span></span>
<span class="line"><span class="LineNumber">  10 </span><span class="String">    """</span></span>
<span class="line"><span class="LineNumber">  11 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">threadNum</span><span class="Operator">,</span> <span class="ID">window</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  12 </span>        <span class="ID">threading</span><span class="Operator">.</span><span class="ID">Thread</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  13 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">threadNum</span> <span class="Operator">=</span> <span class="ID">threadNum</span></span>
<span class="line"><span class="LineNumber">  14 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">window</span> <span class="Operator">=</span> <span class="ID">window</span></span>
<span class="line"><span class="LineNumber">  15 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">timeToQuit</span> <span class="Operator">=</span> <span class="ID">threading</span><span class="Operator">.</span><span class="ID">Event</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  16 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">timeToQuit</span><span class="Operator">.</span><span class="ID">clear</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  17 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">messageCount</span> <span class="Operator">=</span> <span class="ID">random</span><span class="Operator">.</span><span class="ID">randint</span><span class="Operator">(</span><span class="Number">10</span><span class="Operator">,</span><span class="Number">20</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  18 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">messageDelay</span> <span class="Operator">=</span> <span class="Number">0.1</span> <span class="Operator">+</span> <span class="Number">2.0</span>
<span class="Operator">*</span> <span class="ID">random</span><span class="Operator">.</span><span class="ID">random</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  19 </span></span>
<span class="line"><span class="LineNumber">  20 </span>    <span class="ResWord">def</span> <span class="ID">stop</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  21 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">timeToQuit</span><span class="Operator">.</span><span class="ID">set</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  22 </span></span>
<span class="line"><span class="LineNumber">  23 </span>    <span class="ResWord">def</span> <span class="ID">run</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span><span class="Comment">#运行一个线程</span></span>
<span class="line"><span class="LineNumber">  24 </span>        <span class="ID">msg</span> <span class="Operator">=</span> <span class="String">"Thread %d iterating %d times with a delay of %1.4f\n"</span> \
<span class="Operator">%</span> <span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">threadNum</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">messageCount</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">messageDelay</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  25 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">CallAfter</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">window</span><span class="Operator">.</span><span class="ID">LogMessage</span><span class="Operator">,</span> <span class="ID">msg</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  26 </span></span>
<span class="line"><span class="LineNumber">  27 </span>        <span class="ResWord">for</span> <span class="ID">i</span> <span class="ResWord">in</span> <span class="ID">range</span><span class="Operator">(</span><span class="Number">1</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">messageCount</span><span class="Operator">+</span><span class="Number">1</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  28 </span>            <span class="ID">self</span><span class="Operator">.</span><span class="ID">timeToQuit</span><span class="Operator">.</span><span class="ID">wait</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">messageDelay</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  29 </span>            <span class="ResWord">if</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">timeToQuit</span><span class="Operator">.</span><span class="ID">isSet</span><span class="Operator">(</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  30 </span>                <span class="ResWord">break</span></span>
<span class="line"><span class="LineNumber">  31 </span>            <span class="ID">msg</span> <span class="Operator">=</span> <span class="String">"Message %d from thread %d\n"</span> <span class="Operator">%</span> <span class="Operator">(</span><span class="ID">i</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">threadNum</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  32 </span>            <span class="ID">wx</span><span class="Operator">.</span><span class="ID">CallAfter</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">window</span><span class="Operator">.</span><span class="ID">LogMessage</span><span class="Operator">,</span> <span class="ID">msg</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  33 </span>        <span class="ResWord">else</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  34 </span>            <span class="ID">wx</span><span class="Operator">.</span><span class="ID">CallAfter</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">window</span><span class="Operator">.</span><span class="ID">ThreadFinished</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  35 </span></span>
<span class="line"><span class="LineNumber">  36 </span></span>
<span class="line"><span class="LineNumber">  37 </span></span>
<span class="line"><span class="LineNumber">  38 </span><span class="ResWord">class</span> <span class="ID">MyFrame</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  39 </span>    <span class="ResWord">def</span> <span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  40 </span>        <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Frame</span><span class="Operator">.</span><span class="ID">__init__</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">None</span><span class="Operator">,</span> <span class="ID">title</span><span class="Operator">=</span><span class="String">"Multi-threaded GUI"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  41 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">threads</span> <span class="Operator">=</span> <span class="Operator">[</span><span class="Operator">]</span></span>
<span class="line"><span class="LineNumber">  42 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">count</span> <span class="Operator">=</span> <span class="Number">0</span></span>
<span class="line"><span class="LineNumber">  43 </span></span>
<span class="line"><span class="LineNumber">  44 </span>        <span class="ID">panel</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Panel</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  45 </span>        <span class="ID">startBtn</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Button</span><span class="Operator">(</span><span class="ID">panel</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"Start a thread"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  46 </span>        <span class="ID">stopBtn</span>  <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">Button</span><span class="Operator">(</span><span class="ID">panel</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"Stop all threads"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  47 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">tc</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">StaticText</span><span class="Operator">(</span><span class="ID">panel</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">"Worker Threads: 00"</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  48 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">log</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">TextCtrl</span><span class="Operator">(</span><span class="ID">panel</span><span class="Operator">,</span> <span class="Operator">-</span><span class="Number">1</span><span class="Operator">,</span> <span class="String">""</span><span class="Operator">,</span></span>
<span class="line"><span class="LineNumber">  49 </span>                               <span class="ID">style</span><span class="Operator">=</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">TE_RICH</span><span class="Operator">|</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">TE_MULTILINE</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  50 </span></span>
<span class="line"><span class="LineNumber">  51 </span>        <span class="ID">inner</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BoxSizer</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">HORIZONTAL</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  52 </span>        <span class="ID">inner</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">startBtn</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">RIGHT</span><span class="Operator">,</span> <span class="Number">15</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  53 </span>        <span class="ID">inner</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">stopBtn</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">RIGHT</span><span class="Operator">,</span> <span class="Number">15</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  54 </span>        <span class="ID">inner</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">tc</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ALIGN_CENTER_VERTICAL</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  55 </span>        <span class="ID">main</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">BoxSizer</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">VERTICAL</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  56 </span>        <span class="ID">main</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">inner</span><span class="Operator">,</span> <span class="Number">0</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">ALL</span><span class="Operator">,</span> <span class="Number">5</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  57 </span>        <span class="ID">main</span><span class="Operator">.</span><span class="ID">Add</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">log</span><span class="Operator">,</span> <span class="Number">1</span><span class="Operator">,</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">EXPAND</span><span class="Operator">|</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">ALL</span><span class="Operator">,</span> <span class="Number">5</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  58 </span>        <span class="ID">panel</span><span class="Operator">.</span><span class="ID">SetSizer</span><span class="Operator">(</span><span class="ID">main</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  59 </span></span>
<span class="line"><span class="LineNumber">  60 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_BUTTON</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnStartButton</span><span class="Operator">,</span> <span class="ID">startBtn</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  61 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_BUTTON</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnStopButton</span><span class="Operator">,</span> <span class="ID">stopBtn</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  62 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Bind</span><span class="Operator">(</span><span class="ID">wx</span><span class="Operator">.</span><span class="ID">EVT_CLOSE</span><span class="Operator">,</span>  <span class="ID">self</span><span class="Operator">.</span><span class="ID">OnCloseWindow</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  63 </span></span>
<span class="line"><span class="LineNumber">  64 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">UpdateCount</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  65 </span></span>
<span class="line"><span class="LineNumber">  66 </span>    <span class="ResWord">def</span> <span class="ID">OnStartButton</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  67 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">count</span> <span class="Operator">+=</span> <span class="Number">1</span></span>
<span class="line"><span class="LineNumber">  68 </span>        <span class="ID">thread</span> <span class="Operator">=</span> <span class="ID">WorkerThread</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">count</span><span class="Operator">,</span> <span class="ID">self</span><span class="Operator">)</span><span class="Comment">#创建一个线程</span></span>
<span class="line"><span class="LineNumber">  69 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">threads</span><span class="Operator">.</span><span class="ID">append</span><span class="Operator">(</span><span class="ID">thread</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  70 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">UpdateCount</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  71 </span>        <span class="ID">thread</span><span class="Operator">.</span><span class="ID">start</span><span class="Operator">(</span><span class="Operator">)</span><span class="Comment">#启动线程</span></span>
<span class="line"><span class="LineNumber">  72 </span></span>
<span class="line"><span class="LineNumber">  73 </span>    <span class="ResWord">def</span> <span class="ID">OnStopButton</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  74 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">StopThreads</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  75 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">UpdateCount</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  76 </span></span>
<span class="line"><span class="LineNumber">  77 </span>    <span class="ResWord">def</span> <span class="ID">OnCloseWindow</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">evt</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  78 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">StopThreads</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  79 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">Destroy</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  80 </span></span>
<span class="line"><span class="LineNumber">  81 </span>    <span class="ResWord">def</span> <span class="ID">StopThreads</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span><span class="Comment">#从池中删除线程</span></span>
<span class="line"><span class="LineNumber">  82 </span>        <span class="ResWord">while</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">threads</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  83 </span>            <span class="ID">thread</span> <span class="Operator">=</span> <span class="ID">self</span><span class="Operator">.</span><span class="ID">threads</span><span class="Operator">[</span><span class="Number">0</span><span class="Operator">]</span></span>
<span class="line"><span class="LineNumber">  84 </span>            <span class="ID">thread</span><span class="Operator">.</span><span class="ID">stop</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  85 </span>            <span class="ID">self</span><span class="Operator">.</span><span class="ID">threads</span><span class="Operator">.</span><span class="ID">remove</span><span class="Operator">(</span><span class="ID">thread</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  86 </span></span>
<span class="line"><span class="LineNumber">  87 </span>    <span class="ResWord">def</span> <span class="ID">UpdateCount</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">)</span><span class="Operator">:</span></span>
<span class="line"><span class="LineNumber">  88 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">tc</span><span class="Operator">.</span><span class="ID">SetLabel</span><span class="Operator">(</span><span class="String">"Worker
Threads: %d"</span> <span class="Operator">%</span> <span class="ID">len</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">.</span><span class="ID">threads</span><span class="Operator">)</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  89 </span></span>
<span class="line"><span class="LineNumber">  90 </span>    <span class="ResWord">def</span> <span class="ID">LogMessage</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">msg</span><span class="Operator">)</span><span class="Operator">:</span><span class="Comment">#注册一个消息</span></span>
<span class="line"><span class="LineNumber">  91 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">log</span><span class="Operator">.</span><span class="ID">AppendText</span><span class="Operator">(</span><span class="ID">msg</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  92 </span></span>
<span class="line"><span class="LineNumber">  93 </span>    <span class="ResWord">def</span> <span class="ID">ThreadFinished</span><span class="Operator">(</span><span class="ID">self</span><span class="Operator">,</span> <span class="ID">thread</span><span class="Operator">)</span><span class="Operator">:</span><span class="Comment">#删除线程</span></span>
<span class="line"><span class="LineNumber">  94 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">threads</span><span class="Operator">.</span><span class="ID">remove</span><span class="Operator">(</span><span class="ID">thread</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  95 </span>        <span class="ID">self</span><span class="Operator">.</span><span class="ID">UpdateCount</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  96 </span></span>
<span class="line"><span class="LineNumber">  97 </span></span>
<span class="line"><span class="LineNumber">  98 </span><span class="ID">app</span> <span class="Operator">=</span> <span class="ID">wx</span><span class="Operator">.</span><span class="ID">PySimpleApp</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber">  99 </span><span class="ID">frm</span> <span class="Operator">=</span> <span class="ID">MyFrame</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 100 </span><span class="ID">frm</span><span class="Operator">.</span><span class="ID">Show</span><span class="Operator">(</span><span class="Operator">)</span></span>
<span class="line"><span class="LineNumber"> 101 </span><span class="ID">app</span><span class="Operator">.</span><span class="ID">MainLoop</span><span class="Operator">(</span><span class="Operator">)</span><span class="Text"></span></span>
</pre>
</div>
<span id="line-902" class="anchor"></span><span id="line-903" class="anchor"></span><p class="line862">上面这个例子使用了<tt>Python</tt>的<tt>threading</tt>模块。上面的代码使用<tt>wx.CallAfter(func,</tt>*<tt>args)</tt>传递方法给主线程。这将发送一个事件给主线程，之后，事件以标准的方式被处理，并触发对<tt>func(</tt>*<tt>args)</tt>的调用。因些，在这种情况中，线程在它的生命周期期间调用<tt>LogMessage()</tt>，并在线程结束前调用<tt>ThreadFinished()</tt>。
<span id="line-904" class="anchor"></span><span id="line-905" class="anchor"></span><span id="line-906" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-4ffaff2d673b3eeb0f2f5b60b781d12c5f1bbf89">6.2. 使用队列对象管理线程的通信</h3>
<span id="line-907" class="anchor"></span><span id="line-908" class="anchor"></span><span id="line-909" class="anchor"></span><p class="line862">尽管使用<tt>CallAfter()</tt>是管理线程通信的最简单的方法，但是它并不是唯一的机制。你可以使用<tt>Python</tt>的线程安全的队列对象去发送命令对象给<tt>UI</tt>线程。这个<tt>UI</tt>线程应该在<tt>wx.EVT_IDLE</tt>事件的处理函数中写成需要接受来自该队列的命令。 <span id="line-910" class="anchor"></span><span id="line-911" class="anchor"></span></p>
<p class="line862">本质上，你要为线程通信设置一个并行的事件队列。如果使用这一方法，那么工作线程在当它们增加一个命令对象到队列时，应该调用全局函数<tt>wx.WakeUpIdle()</tt>以确保尽可能存在在一个空闲事件。这个技术比<tt>wx.CallAfter()</tt>更复杂，但也更灵活。特别是，这个机制可以帮助你在后台线程间通信，虽然所有的<tt>GUI</tt>处理仍在主线程上。
<span id="line-912" class="anchor"></span><span id="line-913" class="anchor"></span><span id="line-914" class="anchor"></span></p>
<p class="line867">
</p>
<h3 id="head-123adfd8307a973e494ce7f2c8c3c499fd323f71">6.3. 开发你自已的解决方案</h3>
<span id="line-915" class="anchor"></span><span id="line-916" class="anchor"></span><span id="line-917" class="anchor"></span><p class="line862">你也可以让你自己的工作线程创建一个<tt>wxPython</tt>事件（标准的或自定义的），并使用全局函数<tt>wx.PostEvent(window,
event)</tt>将它发送给<tt>UI</tt>线程中的一个特定的窗口。该事件被添加到特定窗口的未决事件队列中，并且<tt>wx.WakeUpIdle</tt>自动被调用。这条道的好处是事件将遍历的<tt>wxPython</tt>事件设置，这意味你将自由地得到许多事件处理能力，坏处是你不得不自已管理所有的线程和<tt>wx.CallAfter()</tt>函数所为你做的事件处理。
<span id="line-918" class="anchor"></span><span id="line-919" class="anchor"></span><span id="line-920" class="anchor"></span></p>
<p class="line867">
</p>
<h2 id="head-d9851afa62c33c0ea19a78d12a632ce1bc636d30">7. 本章小结</h2>
<span id="line-921" class="anchor"></span><span id="line-922" class="anchor"></span><span id="line-923" class="anchor"></span><p class="line862">1、拖放和剪贴板事件是非常相似的，两者都使用了<tt>wx.DataObject</tt>来作为数据格式的媒介。除了可以创建自定义的格式以外，还存在着默认的数据对象，包括文本，文件和位图。在剪贴板的使用中，全局对象<tt>wx.TheClipboard</tt>管理数据的传送并代表底层系统的剪贴板。
<span id="line-924" class="anchor"></span><span id="line-925" class="anchor"></span></p>
<p class="line874">2、对于拖放操作，拖动源和拖动到的目标一起工作来管理数据传送。拖动源事件被阻塞直到拖动到的目标作出该拖动操作是否有效的判断。 <span id="line-926" class="anchor"></span><span id="line-927" class="anchor"></span></p>
<p class="line862">3、类<tt>wx.Timer</tt>使你能够设置定时的事件。 <span id="line-928" class="anchor"></span><span id="line-929" class="anchor"></span></p>
<p class="line862">4、线程在<tt>wxPython</tt>是可以实现的，但时确保所有的<tt>GUI</tt>活动发生在主线程中是非常重要的。你可以使用函数<tt>wx.CallAfter()</tt>来管理内部线程的通信问题。 <span id="line-930" class="anchor"></span><span id="line-931" class="anchor"></span><span id="line-932" class="anchor"></span><span id="line-933" class="anchor"></span><span id="line-934" class="anchor"></span><span id="bottom" class="anchor"></span></p>
</div>
</div>
</td>
                    </tr></tbody></table>
</div>
        
        
        
        <td id="portal-column-two">
            <div class="visualPadding">
                <!-- The sticky notes -->
                 
            </div>
        </td>
        
        
        
        <div class="visualClear" id="clear-space-before-footer">
            <!-- -->
        </div>
        <hr class="netscape4">
<div id="portal-footer">
            <p> 中国Zope/Plone用户组 - 站长  ： <a target="_self" href="http://czug.org/blog/panjy"> 潘俊勇 </a>、
             <a target="_self" href="http://czug.org/blog/zhangbingkai"> 张炳凯 </a>
            </p>
        </div>
        <div id="portal-colophon">

            <p>
            本站由<a target="_blank" href="http://zopen.cn" alt="润普公司 内容管理 文档管理 知识管理 项目管理 Zope Plone">
                   润普公司</a>资助，
            采用 <a href="http://opensource.everydo.com/cms/">易度CMS </a> 构建。</p>

            <p class="discreet">
            广而告之：润普公司 <a href="http://everydo.com">易度云办公平台</a>，包括<a href="http://edodocs.com"> 易度文档管理系统 </a>，<a href="http://everydo.com/themes/czug.org/pm.everydo.com%0A">易度项目管理系统 </a> , <a href="http://everydo.com/tour/dept.rst"> 易度部门管理 </a> ，均采用Zope 3/BFG技术开发。
            <br><a target="_blank" href="http://www.miibeian.gov.cn/">沪ICP备05008050</a>
            </p>
            </div>
        
        
        <script type="text/javascript">
        var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
        document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
        </script><script type="text/javascript">
        try {
        var pageTracker = _gat._getTracker("UA-13170837-4");
        pageTracker._trackPageview();
        } catch(err) {}</script>
</body>
</html>
